- Home
- Subjects
- Data Science
- SQL for Data Analysis
SQL for Data Analysis MCQs · Page 2
75 multiple choice questions with answers in Data Science.
-
A
It forms implicit groups from every selected column of the query output
-
B
It filters the single implicit group, returning either one row or no rows at all
Correct answer
-
C
It is a syntax error, because HAVING always requires an explicit GROUP BY clause
-
D
It filters individual rows before aggregation, exactly as a WHERE clause would
-
A
CAST(order_date AS TEXT) LIKE '2024%', which cannot use the index
-
B
order_date + INTERVAL '1 day' = '2024-06-01', a computed date
-
C
order_date >= '2024-01-01' AND order_date < '2025-01-01'
Correct answer
-
D
EXTRACT(YEAR FROM order_date) = 2024, which scans every row
-
A
TRUNCATE accepts a WHERE clause but is slower than DELETE, because it scans the whole table row by row
-
B
Both fire row-level triggers once per removed row, so auditing behaves exactly the same for the two statements here
-
C
DELETE cannot be rolled back in any database, while TRUNCATE always can, because it is DDL and not DML
-
D
TRUNCATE removes all rows in one operation, cannot take a WHERE clause and fires no row-level delete triggers
Correct answer
-
A
The store's average sales, with 0 substituted wherever the partition is empty or has one row
-
B
The next month's sales within the same store, with 0 substituted for the last row of each partition
-
C
The running total of sales within each store, starting at 0 at the very first row of every partition here
-
D
The previous month's sales within the same store, with 0 substituted for the first row of each partition
Correct answer
-
A
Because NOT IN converts the NULL to zero and then compares numerically, matching every zero value
-
B
Because x <> NULL is UNKNOWN, so the NOT IN predicate can never be TRUE; NOT EXISTS is null-safe
Correct answer
-
C
Because the subquery is executed only when it contains no NULL values, and otherwise the whole subquery is skipped
-
D
Because NOT IN is not valid with a subquery at all, so the parser rejects the statement outright
-
A
UNION removes duplicate rows and therefore usually needs a sort or hash, while UNION ALL concatenates and keeps duplicates
Correct answer
-
B
Both remove duplicate rows, differing only in the column ordering they produce, so the choice between them is purely cosmetic
-
C
UNION requires matching column names, while UNION ALL does not, which is why UNION ALL is faster to write
-
D
UNION ALL removes duplicate rows, while UNION keeps them, so the two operators are exact opposites in effect
-
A
20, because the median of the three values is used instead of the mean
-
B
NULL, because any NULL in the input makes the aggregate NULL
-
C
15, because the NULL is excluded from both the sum and the count
Correct answer
-
D
10, because the NULL is treated as a zero value in the average
-
A
The rows are excluded from the result entirely, because the grouping key is missing
-
B
The query fails unless NULLs are filtered out first, since grouping needs a known key
-
C
All NULLs form a single group, because GROUP BY treats them as equal to each other
Correct answer
-
D
Each NULL forms its own separate group, because NULL is never equal to another one
-
A
Because COUNT(*) ignores NULLs in the customer key, so rows with a missing key are never counted at all by the aggregate
-
B
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
-
C
Because the join sorts the rows and duplicates them during the sort, so the sort key must therefore be made distinct first of all
-
D
Because COUNT(*) counts columns rather than rows, so it returns the width of the joined result instead of its height
-
A
It includes them only if the column is indexed, since the index stores a default value
-
B
It includes them, because NULL differs from every value including the string x
-
C
It excludes them, because NULL <> 'x' evaluates to UNKNOWN rather than TRUE
Correct answer
-
D
It raises an error unless col is declared NOT NULL, because the comparison is invalid
-
A
Nothing changes, because the optimiser moves the predicate into the ON clause
-
B
All inactive matches are kept while unmatched rows are duplicated
-
C
The query fails because WHERE cannot reference the right table
-
D
Rows with no matching b row are removed, turning the outer join into an effective inner join
Correct answer
-
A
1,2,3 for all three functions, because every row receives a distinct sequential position
-
B
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
-
C
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
-
D
1,1,3 for all three, since ties always skip a position whatever function is used
-
A
RANGE requires a PARTITION BY clause, whereas ROWS can be used over the whole table without one
-
B
ROWS ignores the ORDER BY, whereas RANGE sorts the partition before summing
-
C
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
-
D
RANGE includes only the current row, whereas ROWS accumulates every preceding row in the partition
-
A
10 and 7
Correct answer
-
B
7 and 7
-
C
10 and 10
-
D
7 and 10
-
A
93.33, because the denominator includes the NULL row
-
B
NULL, because any NULL makes the average unknown
-
C
90, because the NULL row is ignored in both parts
Correct answer
-
D
60, because NULL is counted as zero
-
A
Whether the subquery returns exactly one row
-
B
Whether the orders table contains a column literally named 1
-
C
Whether every customer has the same order count
-
D
Whether at least one matching order exists per customer
Correct answer
-
A
UNION sorts the output; UNION ALL reverses the sort
-
B
UNION removes duplicate rows; UNION ALL keeps every row
Correct answer
-
C
UNION keeps duplicates; UNION ALL removes them
-
D
UNION works on columns; UNION ALL only on single values
-
A
NULL is a reserved keyword and causes a syntax error
-
B
The column must be cast to text before comparison
-
C
A comparison with NULL is unknown, never true
Correct answer
-
D
NULL is treated as zero in comparisons
-
A
NULL, because the first argument is NULL
-
B
the last argument, unknown
-
C
a comma-separated list of all its arguments
-
D
the first non-NULL argument, a@b.com
Correct answer
-
A
Both assign identical numbers in every ordering
-
B
RANK skips ties and numbers all rows sequentially
-
C
ROW_NUMBER gives 2 and 3; RANK gives 2 and 2
Correct answer
-
D
ROW_NUMBER gives 2 and 2; RANK gives 2 and 3
-
A
Unmatched rows are kept with a NULL status
-
B
Unmatched rows are kept with their status set to the string 'paid'
-
C
The query returns no rows at all
-
D
The query behaves like an inner join, dropping unmatched orders
Correct answer
-
A
One row per department with the sum of salaries
-
B
One row per department with its employee count
Correct answer
-
C
One row per employee with the department name
-
D
A single row with the total employee count
-
A
Only the cities that appear exactly once
-
B
The count of customers in each city
-
C
Each city value once, with duplicates removed
Correct answer
-
D
Every customer row with the city column added
-
A
ORDER BY amount DESC
Correct answer
-
B
ORDER BY amount ASC
-
C
GROUP BY amount DESC
-
D
SORT BY amount DESC
-
A
All rows from the left table, matched or not
-
B
All rows from both tables, matched or not
-
C
Only rows that exist in the left table alone
-
D
Only rows that have a match in both tables
Correct answer