Interview questions
Predict the result, find the errors, explain the solution. You can read the questions without an account.
Level: MiddleReset all
142 questions
How many rows does a
FULL JOINreturn?Predict the resultJOIN and row countsWhat happens when you add a filter on the right table to the
WHEREclause of aLEFT JOIN?Predict the resultJOIN and row countsDoes a condition on the left table in
ONfilter out rows from aLEFT JOIN?Predict the resultJOIN and row countsWhat happens to
SUMafter aJOINwith order line items?Predict the resultJOIN and row countsWhich departments will this query identify as having no employees?
Predict the resultJOIN and row countsFind the bugs: count paid orders for every customer
Find the errorsJOIN and row countsFind the bugs: count orders and units purchased by customer
Find the errorsJOIN and row countsFind the bugs: total units purchased by each customer
Find the errorsJOIN and row countsWhy does a condition in
WHEREturn aLEFT JOINinto an inner join?ExplainJOIN and row countsHow many rows will two consecutive
LEFT JOINoperations produce?Predict the resultJOIN and row countsWhich paid orders remain after excluding orders containing
pen?Predict the resultJOIN and row countsHow many
login-to-viewpairs will a join create for each user?Predict the resultJOIN and row countsFind the bugs: counting line items in a customer's orders
Find the errorsJOIN and row countsFind the bugs: customers with three different products
Find the errorsJOIN and row countsWhat's the difference between
LEFT SEMI JOIN,LEFT ANTI JOIN, andLEFT ANY JOINin ClickHouse?ExplainJOIN and row countsHow does
AVGhandleNULL?Predict the resultNULLWhat does
NOT INreturn when a subquery containsNULL?Predict the resultNULLWill
city = 'Berlin'andcity <> 'Berlin'include every customer?Predict the resultNULLWho does
ORDER BY salary DESC LIMIT 1return?Predict the resultNULLHow many groups does
GROUP BYcreate for a column containing multipleNULLvalues?Predict the resultNULLFind the bugs: paid orders excluding the test account
Find the errorsNULLWhat is three-valued logic in SQL?
ExplainNULLFind the bugs: categorizing events by user
Find the errorsNULLWhat does
GREATESTreturn for two employee salaries?Predict the resultNULLHow many paying customers does
COUNT DISTINCT CASEcount?Predict the resultNULLWhat do the two window calculations return for the city values?
Predict the resultNULLWhich values remain after
EXCEPT ALL?Predict the resultNULLFind the bugs: matching values from two sources
Find the errorsNULLCan a foreign key contain
NULL?ExplainNULLHow many rows does a window function see when the query includes
WHERE?Predict the resultQuery execution orderCan you use an aggregate alias in
HAVING?Predict the resultQuery execution orderFind the bugs: the most recently hired employee in each department
Find the errorsQuery execution orderFind the bugs: departments with at least two hires since 2021
Find the errorsQuery execution orderFind the bugs: paid orders report
Find the errorsQuery execution orderWhat happens when
LIMITis inside a subquery?Predict the resultQuery execution orderWill the ranking match the output order?
Predict the resultQuery execution orderWhat happens when you combine
DISTINCTwith a windowCOUNT?Predict the resultQuery execution orderWhat does
SELECT DISTINCT COUNT(*)return withGROUP BY?Predict the resultQuery execution orderFind the bugs: total value of orders containing a specific product
Find the errorsQuery execution orderCan you sort groups by an aggregate that isn't in
SELECT?ExplainQuery execution orderWhat does
WHEREfilter, and what doesHAVINGfilter?Predict the resultGROUP BY / HAVINGWill a customer with no paid orders disappear if the condition is inside
SUM?Predict the resultGROUP BY / HAVINGCan you add up unique user counts across groups?
Predict the resultGROUP BY / HAVINGWhat proportion of paid orders does
SUM(…) / COUNT(*)calculate?Predict the resultGROUP BY / HAVINGFind the bugs: units ordered and average unit price
Find the errorsGROUP BY / HAVINGFind the bugs: calculating company-wide average salary from department averages
Find the errorsGROUP BY / HAVINGHow do you calculate the average price, and why does
AVG(price)give the wrong result here?ExplainGROUP BY / HAVINGWhat happens when you cap each order versus the group total?
Predict the resultGROUP BY / HAVINGHow do you safely calculate a ratio of two aggregates?
ExplainGROUP BY / HAVINGHow many rows does
HAVINGreturn withoutGROUP BY?Predict the resultGROUP BY / HAVINGHow many groups and orders does the outer query count?
Predict the resultGROUP BY / HAVINGFind the bugs: revenue from repeat customers
Find the errorsGROUP BY / HAVINGFind the bugs: departments with payroll above the threshold
Find the errorsGROUP BY / HAVINGHow do you combine product names into one string per order?
ExplainGROUP BY / HAVINGWhat happens to a running total when the window's
ORDER BYcontains ties?Predict the resultWindow functionsWhat does
LAST_VALUEreturn with the default window frame?Predict the resultWindow functionsHow does
NTILE(4)split 10 rows?Predict the resultWindow functionsWill a query using
QUALIFYrun?Predict the resultWindow functionsFind the bugs: three-order moving average
Find the errorsWindow functionsFind the bugs: an employee's percentage of department payroll
Find the errorsWindow functionsHow do you calculate month-over-month revenue growth?
ExplainWindow functionsFind the bugs: Count a user's earlier events
Find the errorsWindow functionsWhat does SUM return with a ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING frame?
Predict the resultWindow functionsFind the bugs: Order number and total orders per customer
Find the errorsWindow functionsWhat percentage ranks will
PERCENT_RANKassign based on employee salaries?Predict the resultWindow functionsWhat does
RANKreturn when orders are grouped by status?Predict the resultWindow functionsWhich names will
NTH_VALUEreturn when ordered by hire date?Predict the resultWindow functionsHow do you analyze an event sequence with
LAGandLEAD?ExplainWindow functionsHow many rows will
DENSE_RANKreturn for the top 2 salaries in each department?Predict the resultDuplicates and top NWill an event without a user appear in the “latest event per user” results?
Predict the resultDuplicates and top NWhich order will
DISTINCT ONkeep for each customer?Predict the resultDuplicates and top NFind the bugs: Which rows remain after deduplication?
Find the errorsDuplicates and top NFind the bugs: Highest-paid employees in each department
Find the errorsDuplicates and top NHow do you find and remove duplicates while keeping one row?
ExplainDuplicates and top NWhich rows will a query with
GROUP BYandLIMITreturn?Predict the resultDuplicates and top NWhat will a query using
DISTINCTandUNION ALLreturn?Predict the resultDuplicates and top NFind the bugs: Each customer's latest paid order
Find the errorsDuplicates and top NFind the bugs: Top two products by units sold
Find the errorsDuplicates and top NWhich rows pass the running total filter?
Predict the resultDuplicates and top NWill
DATE_TRUNC('month', created_at)run successfully?Predict the resultDatesHow many days does date subtraction return between January 31 and February 1?
Predict the resultDatesOn how many days was each user active?
Predict the resultDatesWhat does
created_at + INTERVAL '1' MONTHreturn for orders placed at the end of a month?Predict the resultDatesFind the bugs: DAU for the last three days
Find the errorsDatesFind the bugs: employees with at least five years of service
Find the errorsDatesWhich users were active on both sides of the month boundary?
Predict the resultDatesHow many earlier paid orders does a temporal
SELF JOINfind?Predict the resultDatesWhat does grouping events by hour return?
Predict the resultDatesWhich orders were placed after each user's last event?
Predict the resultDatesFind the bugs: customer events before an order
Find the errorsDatesFind the bugs: customers whose first order was in March
Find the errorsDatesCan a subquery in
FROMrun without an alias?Predict the resultSubqueries, CTEs, EXISTS / IN, UNIONWhich orders does
EXISTSfind when checking for a later order?Predict the resultSubqueries, CTEs, EXISTS / IN, UNIONHow many customers does
INfind when the subquery returnsNULL?Predict the resultSubqueries, CTEs, EXISTS / IN, UNIONHow many rows does
UNION ALLreturn withLIMIT 3at the end?Predict the resultSubqueries, CTEs, EXISTS / IN, UNIONHow many rows will
INTERSECTreturn for paid orders and customers fromBerlin?Predict the resultSubqueries, CTEs, EXISTS / IN, UNIONFind the bugs: the highest-priced item in each order
Find the errorsSubqueries, CTEs, EXISTS / IN, UNIONFind the bugs: employee reporting hierarchy
Find the errorsSubqueries, CTEs, EXISTS / IN, UNIONHow does
INwith a subquery differ fromEXISTS?ExplainSubqueries, CTEs, EXISTS / IN, UNIONWhich orders have totals above the average for paid orders?
Predict the resultSubqueries, CTEs, EXISTS / IN, UNIONWhich customers pass the aggregate filter?
Predict the resultSubqueries, CTEs, EXISTS / IN, UNIONWhich customers have only paid orders?
Predict the resultSubqueries, CTEs, EXISTS / IN, UNIONWhich department will the nested
MAXselect?Predict the resultSubqueries, CTEs, EXISTS / IN, UNIONFind the bugs: customers with multiple paid orders
Find the errorsSubqueries, CTEs, EXISTS / IN, UNIONFind the bugs: three required event types
Find the errorsSubqueries, CTEs, EXISTS / IN, UNIONHow many rows will a name search return if you remove
LOWER?Predict the resultIndexesFind the bugs: the latest paid and cancelled orders
Find the errorsIndexesFind the bugs: retrieving items from two orders
Find the errorsIndexesFind the bugs: Exporting employee records for HR
Find the errorsIndexesHow would you index JSONB searches in PostgreSQL?
ExplainIndexesDoes a foreign key automatically create an index?
ExplainIndexesWhen is a partial index useful in PostgreSQL?
ExplainIndexesHow many rows will an
ORquery return when rewritten withUNION ALL?Predict the resultEXPLAIN and optimizationFind the bugs: the second page of a feed without
OFFSETFind the errorsEXPLAIN and optimizationWhy did the planner choose
Seq Scaneven though an index exists?ExplainEXPLAIN and optimizationHow do you read an
EXPLAIN ANALYZEplan in PostgreSQL?ExplainEXPLAIN and optimizationWhy does
OFFSETpagination slow down, and what can replace it?ExplainEXPLAIN and optimizationFind the bugs: high-value paid orders from two customers
Find the errorsEXPLAIN and optimizationFind the bugs: orders containing two product types
Find the errorsEXPLAIN and optimizationHow do
Nested Loop,Hash Join, andMerge Joindiffer?ExplainEXPLAIN and optimizationWhy does the planner get row-count estimates wrong?
ExplainEXPLAIN and optimizationWhen does it make sense to aggregate data before a
JOIN?ExplainEXPLAIN and optimizationHow can you tell which table partitions were actually scanned?
ExplainEXPLAIN and optimizationWill
FOR UPDATE SKIP LOCKEDwork withoutBEGIN?Predict the resultTransactions and locksFind the bugs: long-running transactions
Find the errorsTransactions and locksWhat are the transaction isolation levels, and which anomalies do they prevent?
ExplainTransactions and locksWhat can go wrong when two withdrawals hit the same account at once?
ExplainTransactions and locksHow would you handle two managers editing the same customer record?
ExplainTransactions and locksFind the bugs: selecting orders for processing
Find the errorsTransactions and locksWhat happens when you lock an order and its line items?
Predict the resultTransactions and locksWhy use
SAVEPOINTand partial rollbacks?ExplainTransactions and locksHow do you avoid losing an event when saving an order?
ExplainTransactions and locksWhat's the difference between waiting for a lock,
NOWAIT, andSKIP LOCKED?ExplainTransactions and locksWhat happens to an order ID when a transaction rolls back?
ExplainTransactions and locksFind the bugs: orders with totals that don't match their line items
Find the errorsDDL, keys, and normalizationWhat is normalization, and when should you denormalize?
ExplainDDL, keys, and normalizationHow would you design a schema for scheduling future tasks in local time?
ExplainDDL, keys, and normalizationHow do you insert rows with circular foreign keys?
ExplainDDL, keys, and normalizationHow would you design a many-to-many relationship?
ExplainDDL, keys, and normalizationWhen should you use
ENUM,CHECK, or a status lookup table?ExplainDDL, keys, and normalization