Beginner
Question 1
From the users table, retrieve only the name and city columns.
📖 ExplanationReference
Basic syntax of the SELECT statement
To retrieve data in SQL, you use a SELECT statement. You specify "which columns" to get "from which table."
SELECT columnName FROM tableName;
Selecting specific columns
You can retrieve only the columns you need. For multiple columns, separate them with commas.
SELECT name, city FROM users;
Getting every column (the asterisk)
Using * (asterisk) retrieves every column.
SELECT * FROM users;
⚠️ A common mistake: overusing SELECT *
SELECT * is convenient, but on a table with 100 columns it also pulls back data you don't need, which slows things down. In real-world work, it's good practice to specify only the columns you need.
Giving a column an alias (AS)
You can use AS to give a column an alias. This is handy when you want a more readable name for a report or a screen display.
SELECT name AS user_name, city AS city_name FROM users;
Removing duplicates (DISTINCT)
Using DISTINCT lets you retrieve values with duplicates removed.
SELECT DISTINCT category FROM products;
💼 Where it's used in the real world
SELECT DISTINCT is handy when you want to know "what kinds of values exist." For example, it's often used to check "what status values actually appear in this table?"AS is also commonly used in real work when exporting to CSV or generating reports, to give columns more descriptive names.