1. Home
  2. Subjects
  3. Data Science
  4. SQL for Data Analysis

SQL for Data Analysis MCQs

75 multiple choice questions with answers in Data Science.

Practise SQL for Data Analysis MCQs with answers on SELECT queries, joins, grouping, window functions, subqueries and aggregation. Each question comes with a short explanation, so you understand why an option is correct. A good fit for computer science students and interview preparation.

Showing 1–25 of 75

Why does LAST_VALUE(x) OVER (ORDER BY ts) usually return the current row's value rather than the final value in the partition?

  1. The window is implicitly partitioned by ts, so each row is its own group
  2. The function is nondeterministic and may return any row of the whole partition
  3. The default frame ends at the current row, so the last visible value is here Correct answer
  4. LAST_VALUE ignores the frame and evaluates over the whole peer group
MCQSEXAM Read explanation

SELECT city FROM a EXCEPT SELECT city FROM b; is run where both sides contain a NULL city. How are those NULLs treated?

  1. They are treated as unknown, so every NULL row survives in the resulting output
  2. They abort the query, because set operators reject NULL values in any column
  3. They are duplicated, since set operators never collapse duplicate NULLs
  4. They are treated as equal, so the NULL row is removed from the difference Correct answer
MCQSEXAM Read explanation

SUM(amount) OVER (PARTITION BY account ORDER BY day) is used as a running total. Which frame does the default produce?

  1. RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which also includes rows tied on day Correct answer
  2. ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, counting only physical rows read
  3. RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, which totals the whole partition
  4. ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING, a reverse running total by default
MCQSEXAM Read explanation

A query divides two integer columns as 7 / 2. What is returned, and how is a decimal result obtained?

  1. An error, because dividing two integers requires an explicit CAST in standard SQL
  2. 3, because integer division truncates; cast one operand to numeric to get 3.5 Correct answer
  3. 3.5, because SQL always performs division in floating point internally
  4. 4, because SQL rounds the exact quotient half up when both operands are ints
MCQSEXAM Read explanation

SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1; is run. What does it return?

  1. Every row whose email appears more than once in the table, one row per duplicate
  2. One row per distinct email, including those that appear exactly once in users
  3. One row per email, with the count replaced by the number of distinct duplicates
  4. One row per duplicated email, with the number of occurrences in the count column Correct answer
MCQSEXAM Read explanation

SELECT dept, AVG(salary) FROM staff WHERE AVG(salary) > 50000 GROUP BY dept; fails. Why?

  1. AVG cannot appear in both the select list and the filter of the same query
  2. The GROUP BY clause must also list AVG(salary) explicitly so the aggregate has a name
  3. Aggregates are only permitted in the select list and in the ORDER BY clause
  4. WHERE runs before grouping, so no aggregate exists yet; HAVING must filter instead Correct answer
MCQSEXAM Read explanation

SELECT COUNT(CASE WHEN status = 'late' THEN 1 END) AS late FROM loans; is executed. How does the result differ from COUNT(*)?

  1. It counts every row, because COUNT ignores the NULL result of the CASE expression
  2. It counts late rows twice, since the CASE duplicates every row that matches
  3. It fails, because COUNT cannot accept a CASE expression as its only argument
  4. It counts only late rows, because the CASE yields NULL for all other statuses Correct answer
MCQSEXAM Read explanation

An expression computes revenue / items and items may be zero. Which construct prevents a division-by-zero error, and how?

  1. NULLIF(items, 0), which returns NULL for a zero divisor so the result becomes NULL Correct answer
  2. COALESCE(items, 0), which substitutes a zero divisor so that the division stays exact
  3. NULLIF(items, 0), which replaces NULL divisors with zero and skips those rows
  4. COALESCE(items, 0), which converts a zero divisor into a NULL before dividing
MCQSEXAM Read explanation

A subquery used with NOT IN returns a NULL among its values. What happens to the outer query's result?

  1. It returns the same rows as NOT EXISTS would, since NULLs are ignored by NOT IN
  2. It raises an error, because NOT IN refuses subqueries that can produce NULL
  3. It returns only the rows whose key is itself NULL, treating NULL as a match
  4. It returns no rows at all, because every comparison with NULL yields unknown Correct answer
MCQSEXAM Read explanation

SELECT price * qty AS total FROM items ORDER BY total DESC; works in PostgreSQL. What is total referring to?

  1. The original column named total, since aliases are never visible to ORDER BY
  2. A positional reference, because total is parsed as the second select expression
  3. A re-evaluated expression, since ORDER BY recomputes price times qty again
  4. The output column alias, which ORDER BY accepts even though WHERE does not Correct answer
MCQSEXAM Read explanation

SELECT * FROM events LIMIT 10; is run repeatedly on a large table. Why can the returned rows differ between executions?

  1. LIMIT samples rows randomly unless a repeatable seed is supplied with the statement
  2. Without ORDER BY no row order is guaranteed, so the plan may return any ten rows Correct answer
  3. LIMIT is applied before the WHERE clause, so different rows qualify on each run
  4. The table's primary key is used implicitly, and keys are regenerated on each read
MCQSEXAM Read explanation

SELECT DISTINCT city, country FROM customers; is executed. What exactly is deduplicated?

  1. Only the city column, because DISTINCT applies to the first selected expression only
  2. Only the country column, since the last expression in the list takes precedence
  3. Both columns independently, producing the cross product of unique values
  4. Whole (city, country) pairs, so a city may still appear once per distinct country Correct answer
MCQSEXAM Read explanation

Teacher or blogger? Put this quiz on your website

Free SQL for Data Analysis MCQs quiz with answers for your school site, academy page or blog. Copy one line of code – works on WordPress and Blogger.

Get the quiz widget

All 75 SQL for Data Analysis questions

Show the full list of questions in this topic