Query Data from the Temporary Table
In this step, you will learn how to retrieve data from your temporary table using the SELECT statement with different clauses. Querying data is a core skill for working with databases, as it allows you to view and analyze the information stored.
At the sqlite> prompt, let's first retrieve all data from the temp_employees table. Run this command:
SELECT * FROM temp_employees;
Expected Output:
1|Alice|Sales
2|Bob|Marketing
3|Charlie|Engineering
4|David|Sales
5|Eve|Marketing
6|Frank|Engineering
7|Grace|HR
8|Henry|Sales
The * in SELECT * means "all columns," so this command shows every column and row in the temp_employees table.
Next, filter data using a condition with the WHERE clause. To view only the records for employees in the 'Sales' department, run:
SELECT * FROM temp_employees WHERE department = 'Sales';
Expected Output:
1|Alice|Sales
4|David|Sales
8|Henry|Sales
The WHERE clause narrows down the results to rows that match the condition, in this case, where the department is 'Sales'.
SELECT * FROM temp_employees selects all columns from the temp_employees table.
WHERE department = 'Sales' filters the results to only include rows where the department column is equal to 'Sales'.
Now, sort the data by name using the ORDER BY clause:
SELECT * FROM temp_employees ORDER BY name;
Expected Output:
1|Alice|Sales
2|Bob|Marketing
3|Charlie|Engineering
4|David|Sales
5|Eve|Marketing
6|Frank|Engineering
7|Grace|HR
8|Henry|Sales
The ORDER BY clause sorts the results based on the specified column, in this case, the name column.
Finally, limit the number of results using the LIMIT clause. To view only the first 3 records, run:
SELECT * FROM temp_employees LIMIT 3;
Expected Output:
1|Alice|Sales
2|Bob|Marketing
3|Charlie|Engineering
The LIMIT clause restricts the number of rows returned by the query.