Must-know SQL interview questions
8 reported frequent at Snowflake, Cognizant, Deloitte and 3 more. “Reported” because the company tags have no public source. Every quick answer is free.
- Questions on this page
- 8
- Must know
- 0
- picked by the editor
- Reported frequent
- 8
- company tags, reported
- Companies
- 6
- with a frequent tag
Reported frequent· 8
Tagged as frequent at a company. Reported: the tags have no public source.
- Write a SQL query to find the second highest salary from an Employee table, and explain your approach.Use MAX(salary) WHERE salary < (SELECT MAX(salary)) - portable and duplicate-safe. Or DISTINCT + ORDER BY DESC + LIMIT 1 OFFSET 1. For Nth highest, use DENSE_RANK() OVER (ORDER BY salary DESC) = N, which handles ties.IntermediateFreeReported frequent at Cognizant+1
- Explain the different types of SQL JOINs and write a query to find employees who earn more than the average salary of their department.INNER (matches only), LEFT/RIGHT (all of one side + matches), FULL OUTER (all rows), CROSS (Cartesian), SELF. Use a correlated subquery comparing salary to per-department AVG, or a PARTITION BY window function which scans once.IntermediateFreeReported frequent at Wipro
- SQL: From an Orders table (order_id, customer_id, amount, order_date), write a query to find the top 3 customers by total spend.GROUP BY customer_id, SUM(amount), ORDER BY total DESC, LIMIT 3 (join Customers for names). For ties at 3rd place, use DENSE_RANK() OVER (ORDER BY SUM(amount) DESC) <= 3. Note SQL Server uses TOP/FETCH instead of LIMIT.IntermediateFreeReported frequent at Deloitte
- Find the top three highest-paid employees in each department (including ties on salary).Use DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) in a CTE, then filter rnk <= 3. DENSE_RANK keeps ties and does not skip ranks (unlike RANK/ROW_NUMBER). You must use a CTE/subquery because window functions cannot be filtered in the same-level WHERE.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Snowflake
- Given a table of user login dates, find each user's longest streak of consecutive login days (a gaps-and-islands problem).Gaps-and-islands: per user, ROW_NUMBER ordered by date, then DATEADD(day, -rn, login_date) yields a constant anchor for each consecutive run (both advance in lockstep; a gap breaks it). GROUP BY user, anchor and COUNT gives each streak length; take MAX per user. Dedupe same-day logins first.Advanced₹499 (Pro plan required)Reported frequent at Snowflake
- Write a SQL query to find the Nth highest salary from an Employee table (handling ties), in MySQL, PostgreSQL and SQL Server.Use DENSE_RANK() OVER (ORDER BY salary DESC) and keep the rows where the rank equals N, which treats equal salaries as one rank. Alternatives are DISTINCT with OFFSET N-1 (MySQL needs a literal or a stored function) or a correlated subquery counting distinct higher salaries.IntermediateFreeReported frequent at TCS
- In Snowflake SQL, how do you compute a 7-day rolling average of sales for each day, including days with no sales?Use AVG(daily_total) OVER (ORDER BY sale_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) when there is exactly one row per day. If days are missing, ROWS spans too many calendar days; use a date spine or a RANGE frame with an INTERVAL offset.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Snowflake
- You have a column of semi-structured JSON (a VARIANT) containing an array of order line items. Write a query to extract each line item as its own row with typed columns.Use LATERAL FLATTEN(input => payload:array) to explode the JSON array into rows, then access each element via value:field and cast with ::TYPE. Colon (:) navigates VARIANT paths, double-colon (::) casts; FLATTEN also exposes index/key. This is Snowflake's core semi-structured pattern.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Snowflake