Test your basic understanding of common SQL queries frequently discussed in interviews, including filtering, aggregation, and analytic functions.
Rolling Average Calculation
Which SQL clause allows you to calculate a rolling 7-day average for sales amounts based on sale_date?
- ROLLING_AVG(sales_amount, 7) OVER sale_date
- AVG(sales_amount) PARTITION BY sale_date
- AVG(sales_amount) OVER (ORDER BY sale_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)
- SUM(sales_amount) GROUP BY sale_date
- AVG(sales_amount) WHERE INTERVAL 7 DAY
Second-Highest Value
How would you retrieve the second-highest salary in each department from an employees table?
- MIN(salary) WHERE department_id IN employees
- MAX(salary) WHERE salary u003C (SELECT MAX(salary) ... ) GROUP BY department_id
- SELECT department_id, salary FROM employees
- MAX(salary) WHERE department_id = salary
- SELECT salary WHERE salary = MAX(salary)
Filtering Times
Which SQL WHERE clause would you use to select orders placed between 9:00 AM and 5:00 PM?
- WHERE order_time = '09:00:00' AND '17:00:00'
- WHERE CAST(order_time AS TIME) BETWEEN '09:00:00' AND '17:00:00'
- WHERE order_time u003E= '09:00' OR order_time u003C= '17:00'
- WHERE order_time LIKE '09:00:00-17:00:00%'
- WHERE order_time IN ('09:00:00', '17:00:00')
Identifying Missing Data
If you need to find dates with no sales recorded for each product, which SQL operator should you use to check for missing records after a LEFT JOIN?
- WHERE s.sale_date IS NULL
- WHERE s.sale_date != NULL
- WHERE s.sale_date IS EMPTY
- WHERE s.sale_date = ''
- WHERE s.sale_date NULL
First Order in the Last Month
To find customers who placed their first order in the last month, which clause should you include after a GROUP BY?
- HAVING MIN(order_date) u003E= DATEADD(month, -1, GETDATE())
- GROUP BY customer_id HAVING order_date u003E GETDATE()
- WHERE MIN(order_date) = LAST_MONTH()
- WHERE order_date BETWEEN LAST_MONTH() AND NOW()
- HAVING MAX(order_date) u003C GETDATE()
Multiple Products Per Order
How can you retrieve customer orders containing more than one type of product?
- HAVING SUM(product_id) = 1
- ORDER BY COUNT(product_id)
- HAVING COUNT(DISTINCT product_id) u003E 1
- HAVING COUNT(product_id) u003E 1
- GROUP BY product_id HAVING customer_id u003E 1
Orders Above Average
Which SQL expression identifies orders where the order amount is more than twice the average order amount?
- WHERE order_amount u003E 2 * (SELECT AVG(order_amount) FROM orders)
- WHERE order_amount u003E AVG(order_amount) * 2
- WHERE order_amount u003E 2 AVG(order_amount)
- WHERE AVG(order_amount) u003C order_amount * 2
- WHERE order_amount u003E= DOUBLE AVG(order_amount)
Average Time Between Orders
Which function calculates the previous order date for each customer, enabling you to compute the average number of days between orders?
- PAST_DATE()
- ROW_NUMBER()
- PREVIOUS()
- LEAD()
- LAG()
Highest Revenue Month
Which SQL function can help rank months by revenue within each year to find the highest revenue month per year?
- COUNT() OVER (ORDER BY year)
- RANK() OVER (PARTITION BY year ORDER BY revenue DESC)
- GROUP BY RANK()
- ORDER BY revenue AND year
- RANKING(year, month, revenue)
Grouping for Top Products
To find the most popular product in each category based on sales, which clause should you use for grouping?
- GROUP BY category_id, product_name
- SELECT MAX(sales) BY category_id
- GROUP BY product_id
- PARTITION BY product_name
- GROUP category_id, sales