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

SQL for Data Analysis MCQs · Page 2

75 multiple choice questions with answers in Data Science.

Showing 26–50 of 75

SELECT COUNT(*) FROM orders HAVING COUNT(*) > 100; is valid in PostgreSQL. What does HAVING do when no GROUP BY is written?

  1. It forms implicit groups from every selected column of the query output
  2. It filters the single implicit group, returning either one row or no rows at all Correct answer
  3. It is a syntax error, because HAVING always requires an explicit GROUP BY clause
  4. It filters individual rows before aggregation, exactly as a WHERE clause would
MCQSEXAM Read explanation

Which statement about TRUNCATE compared with DELETE is correct?

  1. TRUNCATE accepts a WHERE clause but is slower than DELETE, because it scans the whole table row by row
  2. Both fire row-level triggers once per removed row, so auditing behaves exactly the same for the two statements here
  3. DELETE cannot be rolled back in any database, while TRUNCATE always can, because it is DDL and not DML
  4. TRUNCATE removes all rows in one operation, cannot take a WHERE clause and fires no row-level delete triggers Correct answer
MCQSEXAM Read explanation

What does LAG(sales, 1, 0) OVER (PARTITION BY store ORDER BY month) return?

  1. The store's average sales, with 0 substituted wherever the partition is empty or has one row
  2. The next month's sales within the same store, with 0 substituted for the last row of each partition
  3. The running total of sales within each store, starting at 0 at the very first row of every partition here
  4. The previous month's sales within the same store, with 0 substituted for the first row of each partition Correct answer
MCQSEXAM Read explanation

Why can WHERE x NOT IN (SELECT y FROM t) return no rows when the subquery yields a NULL?

  1. Because NOT IN converts the NULL to zero and then compares numerically, matching every zero value
  2. Because x <> NULL is UNKNOWN, so the NOT IN predicate can never be TRUE; NOT EXISTS is null-safe Correct answer
  3. Because the subquery is executed only when it contains no NULL values, and otherwise the whole subquery is skipped
  4. Because NOT IN is not valid with a subquery at all, so the parser rejects the statement outright
MCQSEXAM Read explanation

Which statement contrasts UNION and UNION ALL correctly?

  1. UNION removes duplicate rows and therefore usually needs a sort or hash, while UNION ALL concatenates and keeps duplicates Correct answer
  2. Both remove duplicate rows, differing only in the column ordering they produce, so the choice between them is purely cosmetic
  3. UNION requires matching column names, while UNION ALL does not, which is why UNION ALL is faster to write
  4. UNION ALL removes duplicate rows, while UNION keeps them, so the two operators are exact opposites in effect
MCQSEXAM Read explanation

When GROUP BY runs on a column containing NULLs, how are those rows treated?

  1. The rows are excluded from the result entirely, because the grouping key is missing
  2. The query fails unless NULLs are filtered out first, since grouping needs a known key
  3. All NULLs form a single group, because GROUP BY treats them as equal to each other Correct answer
  4. Each NULL forms its own separate group, because NULL is never equal to another one
MCQSEXAM Read explanation

Counting customers by joining customers to orders and using COUNT(*) inflates the number. Why, and what fixes it?

  1. Because COUNT(*) ignores NULLs in the customer key, so rows with a missing key are never counted at all by the aggregate
  2. The one-to-many join repeats each customer row per order, so COUNT(DISTINCT customer_id) or a subquery gives the true count Correct answer
  3. Because the join sorts the rows and duplicates them during the sort, so the sort key must therefore be made distinct first of all
  4. Because COUNT(*) counts columns rather than rows, so it returns the width of the joined result instead of its height
MCQSEXAM Read explanation

A LEFT JOIN carries the right-table condition b.status = 'active' in the WHERE clause instead of the ON clause. What is the effect?

  1. Nothing changes, because the optimiser moves the predicate into the ON clause
  2. All inactive matches are kept while unmatched rows are duplicated
  3. The query fails because WHERE cannot reference the right table
  4. Rows with no matching b row are removed, turning the outer join into an effective inner join Correct answer
MCQSEXAM Read explanation

Values 100, 100, 90 are ranked in descending order. What do RANK, DENSE_RANK and ROW_NUMBER produce?

  1. 1,2,3 for all three functions, because every row receives a distinct sequential position
  2. 1,1,2 for RANK and 1,1,3 for DENSE_RANK, with 1,2,3 for ROW_NUMBER in the very same single query
  3. 1,1,3 for RANK; 1,1,2 for DENSE_RANK; and 1,2,3 for ROW_NUMBER with an arbitrary tie order Correct answer
  4. 1,1,3 for all three, since ties always skip a position whatever function is used
MCQSEXAM Read explanation

In SUM(x) OVER (ORDER BY d), the default window frame uses RANGE. What practical difference does that make compared with an explicit ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW?

  1. RANGE requires a PARTITION BY clause, whereas ROWS can be used over the whole table without one
  2. ROWS ignores the ORDER BY, whereas RANGE sorts the partition before summing
  3. Rows that tie on d are all included in the same running total under RANGE, while ROWS admits them one at a time Correct answer
  4. RANGE includes only the current row, whereas ROWS accumulates every preceding row in the partition
MCQSEXAM Read explanation