πŸŽ“ Velnov

πŸ—„οΈ 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.

Key concepts in this topic

SELECTWildcardBest practicesPerformanceColumnsDISTINCTDuplicatesMultiple columnsAliasesASQuotingExpressionsCalculationsTestingCONCATString functionsLiteralsEscapingSQL injectionClause orderExecution orderUNIONQualified namesTable aliases

Jump to: Easy Β· Medium Β· Hard

Easy questions (4)

Q1. easy

Which query retrieves ALL columns from the students table?

SELECT * FROM students;
  1. A SELECT * FROM students;
  2. B SELECT ALL FROM students;
  3. C GET * FROM students;
  4. D SELECT students;

Answer: A β€” SELECT * FROM students;

The asterisk is a wildcard meaning 'every column'. Handy for exploring a table, but in real code you should list the columns you actually need.

SELECTWildcard

Q3. easy

Which query selects only the name and email columns?

  1. A SELECT name, email FROM students;
  2. B SELECT (name, email) FROM students;
  3. C SELECT name AND email FROM students;
  4. D SELECT name email FROM students;

Answer: A β€” SELECT name, email FROM students;

Separate column names with commas. Option D would be interpreted as aliasing name to 'email'.

SELECTColumns

Q4. easy

What does DISTINCT do?

SELECT DISTINCT city FROM customers;
  1. A Removes duplicate rows from the result
  2. B Sorts the results
  3. C Counts the rows
  4. D Deletes duplicates from the table

Answer: A β€” Removes duplicate rows from the result

DISTINCT filters the RESULT SET only - the stored data is untouched. It applies to the whole selected row, not just the first column.

DISTINCTDuplicates

Q11. easy

Which quotes surround a TEXT VALUE in SQL?

SELECT * FROM customers WHERE city = 'Delhi';
  1. A Single quotes: 'Delhi'
  2. B Double quotes: "Delhi"
  3. C Backticks: `Delhi`
  4. D No quotes needed

Answer: A β€” Single quotes: 'Delhi'

Single quotes mark string literals. Double quotes and backticks are for identifiers (table/column names) - a very common source of confusing errors.

LiteralsQuoting

Medium questions (18)

Q2. medium

Why is SELECT * discouraged in production code?

SELECT id, name, email FROM students;   -- explicit and safe
  1. A It fetches unnecessary data, breaks if columns are added or reordered, and hides intent
  2. B It is a syntax error
  3. C It only returns one column
  4. 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;
  1. A Renames the column in the result (an alias) - the table is unchanged
  2. B Creates a new column in the table
  3. C Casts the data type
  4. 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;
  1. A Wrap it in double quotes "Student Name" (or backticks/brackets depending on the database)
  2. B Use single quotes
  3. C Spaces are not allowed at all
  4. 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?

  1. A Yes - SELECT price * quantity AS total FROM order_items;
  2. B No, only column names are allowed
  3. C Only with a special function
  4. 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;

  1. A A single row with the value 8 - a query needs no table for constant expressions
  2. B A syntax error - FROM is required
  3. C The text '5 + 3'
  4. 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;
  1. A CONCAT(first_name, ' ', last_name) - or first_name || ' ' || last_name in standard SQL
  2. B first_name + last_name always works
  3. C JOIN(first_name, last_name)
  4. 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?

  1. A Double it: 'O''Brien'
  2. B Escape it with a backslash always
  3. C Use double quotes instead
  4. 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?

  1. A SELECT ... FROM ... WHERE ... GROUP BY ... HAVING ... ORDER BY ... LIMIT
  2. B FROM ... SELECT ... WHERE
  3. C WHERE ... SELECT ... FROM
  4. 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;

  1. A Each name plus a literal column containing 'Active' for every row
  2. B Only users whose status is Active
  3. C An error
  4. 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?

  1. A Qualify it: customers.id or an alias like c.id
  2. B Rename the table
  3. C It is impossible
  4. 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;
  1. A FROM customers AS c (the AS is optional: FROM customers c)
  2. B FROM customers = c
  3. C ALIAS customers c
  4. 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?

  1. A It returns NULL for that row
  2. B It throws an error
  3. C It returns 0
  4. 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?

  1. A SELECT *, price * qty AS total FROM orders;
  2. B SELECT * AND total FROM orders;
  3. C SELECT ALL, total FROM orders;
  4. 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?

  1. A Qualify the name: SELECT * FROM sales.orders;
  2. B SELECT * FROM orders IN sales;
  3. C USE sales SELECT * FROM orders;
  4. 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;

  1. A Zero rows - an empty result set, not an error
  2. B NULL
  3. C An error
  4. 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?

  1. A SELECT (passed * 100.0 / total) AS pct FROM stats;
  2. B SELECT passed % total AS pct FROM stats;
  3. C SELECT percent(passed, total) FROM stats;
  4. 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?

  1. A No - SELECT is read-only and always safe to run
  2. B Yes, it locks rows permanently
  3. C Yes, it can delete rows
  4. 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?

  1. A Put each clause on its own line, with keywords in caps and consistent indentation
  2. B Everything on one line
  3. C All lowercase, no line breaks
  4. 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?

  1. A Unique COMBINATIONS of city and country
  2. B Unique cities only
  3. C Unique countries only
  4. 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
  1. A WHERE is evaluated BEFORE SELECT, so the alias does not exist yet
  2. B Aliases only work with strings
  3. C It is a typo
  4. 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;
  1. A Returns different values per row based on conditions - SQL's if/else
  2. B Changes text case
  3. C Creates a new table
  4. 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?

  1. A A single row with the total number of rows in the table
  2. B One row per student
  3. C The number of columns
  4. 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;
  1. A COUNT(*) counts all rows; COUNT(column) counts only rows where that column is NOT NULL
  2. B They are identical
  3. C COUNT(column) is faster always
  4. 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?

  1. A Returns rows that are unique across ALL columns - removing exact duplicate rows
  2. B Removes duplicate values per column independently
  3. C Is invalid syntax
  4. 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;

  1. A Zero rows - the condition is never true
  2. B All rows
  3. C An error
  4. 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?

  1. A Because the SELECT list defines the projection - the shape of the output
  2. B It always uses the table's column order
  3. C It sorts them alphabetically
  4. 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 *

Practice β€œSELECT Basics” as a quiz