ποΈ SQL Β· Topic 2 of 20
SELECT Basics β SQL Questions with Answers
30 practice questions on SELECT Basics, each with the correct answer and a clear explanation of why it is right. Read through them as revision notes, or take this topic as an interactive quiz to test yourself.
4 easy Β· 18 medium Β· 8 hard Β· Part of SQL: Databases from basics to window functions - 20 topics.
Medium questions (18)
Q2. medium
Why is SELECT * discouraged in production code?
SELECT id, name, email FROM students; -- explicit and safe
- A It fetches unnecessary data, breaks if columns are added or reordered, and hides intent
- B It is a syntax error
- C It only returns one column
- D It is slower to type
Answer: A β It fetches unnecessary data, breaks if columns are added or reordered, and hides intent
Naming columns explicitly keeps queries stable when the schema changes, reduces network transfer, and lets the database use covering indexes.
Best practicesPerformance
Q6. medium
What does AS do in: SELECT name AS student_name FROM students;
SELECT price * 1.18 AS price_with_tax FROM products;
- A Renames the column in the result (an alias) - the table is unchanged
- B Creates a new column in the table
- C Casts the data type
- D Sorts by that column
Answer: A β Renames the column in the result (an alias) - the table is unchanged
Aliases make output readable, especially for calculated columns. The keyword AS is optional in most databases but improves clarity.
AliasesAS
Q7. medium
How do you alias a column name that contains a space?
SELECT name AS "Student Name" FROM students;
- A Wrap it in double quotes "Student Name" (or backticks/brackets depending on the database)
- B Use single quotes
- C Spaces are not allowed at all
- D Use a hyphen
Answer: A β Wrap it in double quotes "Student Name" (or backticks/brackets depending on the database)
Standard SQL uses double quotes for identifiers; MySQL uses backticks and SQL Server uses [square brackets]. Single quotes are for string VALUES, not identifiers.
AliasesQuoting
Q8. medium
Can you do arithmetic inside a SELECT?
- A Yes - SELECT price * quantity AS total FROM order_items;
- B No, only column names are allowed
- C Only with a special function
- D Only in WHERE
Answer: A β Yes - SELECT price * quantity AS total FROM order_items;
SELECT can contain expressions: arithmetic, function calls, CASE statements and literals. The computed values appear as columns in the result.
ExpressionsCalculations
Q9. medium
What does this return?
SELECT 5 + 3 AS result;
- A A single row with the value 8 - a query needs no table for constant expressions
- B A syntax error - FROM is required
- C The text '5 + 3'
- D An empty result
Answer: A β A single row with the value 8 - a query needs no table for constant expressions
Most databases (MySQL, PostgreSQL, SQL Server) allow a FROM-less SELECT for testing expressions. Oracle requires FROM dual.
ExpressionsTesting
Q10. medium
How do you join two text columns into one?
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;
- A CONCAT(first_name, ' ', last_name) - or first_name || ' ' || last_name in standard SQL
- B first_name + last_name always works
- C JOIN(first_name, last_name)
- D You cannot combine text
Answer: A β CONCAT(first_name, ' ', last_name) - or first_name || ' ' || last_name in standard SQL
CONCAT works nearly everywhere. The || operator is standard SQL and works in PostgreSQL/Oracle/SQLite; SQL Server uses + for strings.
CONCATString functions
Q12. medium
How do you include an apostrophe inside a string?
- A Double it: 'O''Brien'
- B Escape it with a backslash always
- C Use double quotes instead
- D Apostrophes are impossible
Answer: A β Double it: 'O''Brien'
Doubling the quote is the standard escape. (MySQL also accepts \'.) In application code, use parameterised queries so the driver handles escaping - and prevents SQL injection.
EscapingSQL injection
Q13. medium
What is the logical order in which a simple query is written?
- A SELECT ... FROM ... WHERE ... GROUP BY ... HAVING ... ORDER BY ... LIMIT
- B FROM ... SELECT ... WHERE
- C WHERE ... SELECT ... FROM
- D Any order works
Answer: A β SELECT ... FROM ... WHERE ... GROUP BY ... HAVING ... ORDER BY ... LIMIT
That written order is fixed. Interestingly, the database EXECUTES them in a different order (FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT) - which explains several rules you meet later.
Clause orderExecution order
Q15. medium
What does this query return?
SELECT name, 'Active' AS status FROM users;
- A Each name plus a literal column containing 'Active' for every row
- B Only users whose status is Active
- C An error
- D One row
Answer: A β Each name plus a literal column containing 'Active' for every row
A literal in SELECT creates a constant column. Useful for tagging rows, especially when combining result sets with UNION.
LiteralsUNION
Q16. medium
How do you reference a column when two tables have the same column name?
- A Qualify it: customers.id or an alias like c.id
- B Rename the table
- C It is impossible
- D Use quotes
Answer: A β Qualify it: customers.id or an alias like c.id
Prefixing removes ambiguity. Table aliases keep multi-table queries readable: FROM customers c JOIN orders o ON c.id = o.customer_id.
Qualified namesTable aliases
Q17. medium
How do you give a TABLE an alias?
SELECT c.name, o.total
FROM customers c
JOIN orders o ON c.id = o.customer_id;
- A FROM customers AS c (the AS is optional: FROM customers c)
- B FROM customers = c
- C ALIAS customers c
- D Tables cannot be aliased
Answer: A β FROM customers AS c (the AS is optional: FROM customers c)
Short aliases shorten joins and are required for self-joins, where the same table appears twice.
Table aliasesJoins
Q19. medium
What happens if a CASE expression has no ELSE and nothing matches?
- A It returns NULL for that row
- B It throws an error
- C It returns 0
- D It skips the row
Answer: A β It returns NULL for that row
ELSE is optional but recommended - an unexpected NULL in a report is usually a bug you would rather catch explicitly.
CASENULL
Q20. medium
Which query returns every column of orders plus a computed column?
- A SELECT *, price * qty AS total FROM orders;
- B SELECT * AND total FROM orders;
- C SELECT ALL, total FROM orders;
- D You cannot mix * with expressions
Answer: A β SELECT *, price * qty AS total FROM orders;
You may combine * with additional expressions. With multiple tables, qualify it: SELECT o.*, c.name FROM orders o JOIN customers c ...
SELECT *Expressions
Q22. medium
How do you select rows from a table in another schema?
- A Qualify the name: SELECT * FROM sales.orders;
- B SELECT * FROM orders IN sales;
- C USE sales SELECT * FROM orders;
- D It is not possible
Answer: A β Qualify the name: SELECT * FROM sales.orders;
The full form is database.schema.table. You only need the parts that are not already the current default.
SchemasQualified names
Q23. medium
What does this return if the table is empty?
SELECT name FROM students;
- A Zero rows - an empty result set, not an error
- B NULL
- C An error
- D One blank row
Answer: A β Zero rows - an empty result set, not an error
An empty result is a normal outcome. Applications should handle 'no rows' as a valid case rather than an error.
Empty results
Q25. medium
Which is valid for selecting a computed percentage?
- A SELECT (passed * 100.0 / total) AS pct FROM stats;
- B SELECT passed % total AS pct FROM stats;
- C SELECT percent(passed, total) FROM stats;
- D SELECT passed / total * 100 - always exact
Answer: A β SELECT (passed * 100.0 / total) AS pct FROM stats;
Multiplying by 100.0 (a decimal) forces floating-point division. With two integers, many databases perform INTEGER division, so 3/4 becomes 0 - a classic reporting bug.
Integer divisionCasting
Q27. medium
Does SELECT change the data in the table?
- A No - SELECT is read-only and always safe to run
- B Yes, it locks rows permanently
- C Yes, it can delete rows
- D Only with *
Answer: A β No - SELECT is read-only and always safe to run
This is why you should always preview an UPDATE or DELETE by running the same WHERE clause in a SELECT first.
SafetyBest practices
Q29. medium
Which is the more readable way to write a long query?
- A Put each clause on its own line, with keywords in caps and consistent indentation
- B Everything on one line
- C All lowercase, no line breaks
- D Random formatting
Answer: A β Put each clause on its own line, with keywords in caps and consistent indentation
SQL ignores whitespace, so formatting is purely for humans - and long joins become far easier to review when each clause starts a new line.
StyleReadability
Hard questions (8)
Q5. hard
What does SELECT DISTINCT city, country return?
- A Unique COMBINATIONS of city and country
- B Unique cities only
- C Unique countries only
- D A syntax error
Answer: A β Unique COMBINATIONS of city and country
DISTINCT always applies to the entire row of selected columns. Paris/France and Paris/USA are two distinct rows.
DISTINCTMultiple columns
Q14. hard
Why can you NOT use a column alias in the WHERE clause?
SELECT price * 2 AS dbl FROM p WHERE dbl > 10;
SELECT price * 2 AS dbl FROM p WHERE price * 2 > 10; -- works
SELECT price * 2 AS dbl FROM p ORDER BY dbl; -- also works
- A WHERE is evaluated BEFORE SELECT, so the alias does not exist yet
- B Aliases only work with strings
- C It is a typo
- D You can - it always works
Answer: A β WHERE is evaluated BEFORE SELECT, so the alias does not exist yet
Execution order is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY. Repeat the expression in WHERE, or wrap the query in a subquery. (ORDER BY runs after SELECT, so aliases DO work there.)
Execution orderAliases
Q18. hard
What does a CASE expression do in SELECT?
SELECT name,
CASE
WHEN marks >= 90 THEN 'A'
WHEN marks >= 75 THEN 'B'
ELSE 'C'
END AS grade
FROM students;
- A Returns different values per row based on conditions - SQL's if/else
- B Changes text case
- C Creates a new table
- D Sorts the rows
Answer: A β Returns different values per row based on conditions - SQL's if/else
CASE WHEN ... THEN ... ELSE ... END produces a computed column. It is one of the most useful tools for reporting and bucketing values.
CASEConditional logic
Q21. hard
What does SELECT COUNT(*) FROM students; return?
- A A single row with the total number of rows in the table
- B One row per student
- C The number of columns
- D An error without GROUP BY
Answer: A β A single row with the total number of rows in the table
Aggregate functions with no GROUP BY collapse the whole table into one summary row. COUNT(*) counts rows including those with NULLs.
COUNTAggregates
Q24. hard
What is the difference between COUNT(*) and COUNT(column)?
SELECT COUNT(*) AS total, COUNT(email) AS with_email
FROM customers;
- A COUNT(*) counts all rows; COUNT(column) counts only rows where that column is NOT NULL
- B They are identical
- C COUNT(column) is faster always
- D COUNT(*) ignores NULLs
Answer: A β COUNT(*) counts all rows; COUNT(column) counts only rows where that column is NOT NULL
This difference matters constantly: COUNT(email) tells you how many customers actually provided an email, while COUNT(*) counts everyone.
COUNTNULL
Q26. hard
What does SELECT DISTINCT * FROM t; do?
- A Returns rows that are unique across ALL columns - removing exact duplicate rows
- B Removes duplicate values per column independently
- C Is invalid syntax
- D Returns one row
Answer: A β Returns rows that are unique across ALL columns - removing exact duplicate rows
Only fully identical rows are collapsed. If a table has a primary key, every row is already unique and DISTINCT changes nothing.
DISTINCTDuplicates
Q28. hard
What does this return?
SELECT name FROM students WHERE 1 = 0;
- A Zero rows - the condition is never true
- B All rows
- C An error
- D One row
Answer: A β Zero rows - the condition is never true
WHERE 1=0 is a handy trick to fetch a result's STRUCTURE with no data, e.g. to create an empty copy of a table. Conversely, WHERE 1=1 matches everything and is used when building dynamic queries.
TricksDynamic SQL
Q30. hard
Why does SELECT return columns in the order you list them?
- A Because the SELECT list defines the projection - the shape of the output
- B It always uses the table's column order
- C It sorts them alphabetically
- D The order is random
Answer: A β Because the SELECT list defines the projection - the shape of the output
You control the output shape: reorder, rename and compute columns freely. Only SELECT * inherits the table's physical order - another reason to avoid it in code that depends on position.
ProjectionSELECT *