Query Data with SELECT
In this step, you will learn how to retrieve data from your table using the SELECT statement. 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 users table. Run this command:
SELECT * FROM users;
Expected Output:
1|Alice|alice@example.com
2|Bob|bob@example.com
3|Charlie|charlie@example.com
The * in SELECT * means "all columns," so this command shows every column and row in the users table.
Next, you can choose specific columns to display. To see only the name and email columns, run:
SELECT name, email FROM users;
Expected Output:
Alice|alice@example.com
Bob|bob@example.com
Charlie|charlie@example.com
This command limits the output to just the specified columns, omitting the id.
Finally, filter data using a condition with the WHERE clause. To view only the record for 'Alice', run:
SELECT * FROM users WHERE name = 'Alice';
Expected Output:
1|Alice|alice@example.com
The WHERE clause narrows down the results to rows that match the condition, in this case, where the name is 'Alice'.
SELECT * FROM users selects all columns from the users table.
WHERE name = 'Alice' filters the results to only include rows where the name column is equal to 'Alice'.