SELECT basics: columns, aliases, DISTINCT, LIMIT
SQL · 13 questions
Short notes
Key idea
SELECT picks columns, FROM names the table. Every query you write starts here. You can rename a column with AS, compute new ones, and cut the output with LIMIT.
Syntax
SELECT col1, col2 AS new_name, price * qty AS total
FROM table_name
LIMIT 10;
SELECT *returns every column. Fine for a quick look, not for real queries.DISTINCTafterSELECTremoves duplicate rows.
Example
Show each order's id and its value with tax:
SELECT order_id, amount, amount * 1.18 AS amount_with_tax
FROM orders
LIMIT 5;
The new column exists only in this result; the table is unchanged.
Traps
DISTINCTapplies to the whole row, not just the first column after it.- In standard SQL an alias can't be used in
WHERE, becauseWHEREruns beforeSELECT. DuckDB allows it; PostgreSQL, MySQL and SQL Server don't, so in interviews repeat the expression. - Strings use single quotes; double quotes are for column names.
- Rows have no fixed order without
ORDER BY, soLIMITalone gives "some" rows, not "the first".
Want the full lesson with step-by-step examples, a project and a certificate? SQL course, ₹120 →
- Question 1easy
Which query renames
pricetounit_pricein the result? - Question 2medium
What does
SELECT DISTINCT city, is_premium FROM customersreturn? - Question 3medium
There are 40 customers. 3 have no city recorded, and the rest live in 9 different cities. What does this return?
SELECT COUNT(DISTINCT city) FROM customers;
- Question 4easy
80 orders were placed by customers. How many rows does this return?
SELECT COUNT(*) FROM (SELECT DISTINCT customer_id FROM orders);
- Question 5medium
What does
SELECT product_name price FROM products;return? (Notice there's no comma.)
SELECT basics: columns, aliases, DISTINCT, LIMIT
SELECT basics: columns, aliases, DISTINCT, LIMIT
SELECT basics: columns, aliases, DISTINCT, LIMIT
SELECT basics: columns, aliases, DISTINCT, LIMIT
SELECT basics: columns, aliases, DISTINCT, LIMIT
8 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.