Test your understanding of key SQL concepts and skills commonly assessed in data analyst interviews. This quiz covers joins, indexes, window functions, query optimization, and more.
Finding the Second Highest Salary
Which SQL query would correctly find the second highest salary from a table named 'Employees' with a column 'Salary'?
- SELECT MIN(Salary) FROM Employees;
- SELECT DISTINCT Salary FROM Employees ORDER BY Salary DESC LIMIT 1 OFFSET 1;
- SELECT MAX(Salary) FROM Employees WHERE Salary u003C (SELECT MAX(Salary) FROM Employees);
- SELECT Salary FROM Employees ORDER BY Salary ASC LIMIT 2;
- SELECT COUNT(Salary) FROM Employees;
Types of SQL Joins
What is the difference between an INNER JOIN and a LEFT JOIN in SQL?
- INNER JOIN returns only matching rows, LEFT JOIN returns all left table rows with matched right table rows if available.
- LEFT JOIN is faster than INNER JOIN.
- INNER JOIN returns all records from both tables, LEFT JOIN returns only left table records.
- INNER JOIN ignores NULL values, LEFT JOIN replaces NULL with zeros.
- LEFT JOIN combines every record from both tables regardless of match.
Identifying Duplicates
Which SQL clause combination helps you find duplicate rows in a table based on a 'Name' column?
- WHERE Name = NULL
- GROUP BY Name HAVING COUNT(*) u003E 1
- ORDER BY Name DESC
- LIMIT 2 OFFSET 1
- SELECT DISTINCT Name
Improving Query Performance
What is an effective way to optimize SQL queries for better performance?
- Using indexes on frequently filtered columns
- Running queries during busy hours
- Storing dates as text values
- Adding more columns to SELECT *
- Using as many JOINs as possible
Understanding Window Functions
Which SQL keyword is required to use a window function like ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Salary)?
- WINDOW_CALL
- GROUPED BY
- OVER
- NUM_ROW
- HAVING
Removing Duplicate Records
Which statement removes duplicate records from a table while keeping only unique rows?
- SELECT FROM DISTINCT TableName;
- DELETE FROM TableName WHERE rowid NOT IN (SELECT MIN(rowid) FROM TableName GROUP BY column1, column2);
- REMOVE DUPLICATE TableName;
- CLEAR DUPLICATES TableName;
- UPDATE unique TableName;
WHERE vs. HAVING Clause
What is the main difference between WHERE and HAVING clauses in SQL?
- HAVING cannot use aggregation functions
- HAVING is used before GROUP BY
- WHERE sorts records, HAVING deletes them
- WHERE filters rows before aggregation, HAVING filters groups after aggregation
- WHERE works only with dates, HAVING with numbers
Handling NULL Values
How can you replace NULL values in a column with a default value, for example, converting NULLs to zero in SQL?
- Using the COALESCE(column, 0) function
- REPLACE column WITH 0
- DEFAULT(column, 0)
- WHERE column = NULL
- SKIP NULLS column
Difference Between RANK(), DENSE_RANK(), and ROW_NUMBER()
If two employees have the same salary, how do RANK() and DENSE_RANK() differ when used in an SQL query?
- ROW_NUMBER() and RANK() are identical in all cases
- RANK() leaves a gap after ties, while DENSE_RANK() does not
- RANK() does not handle ties, but DENSE_RANK() does
- Both functions ignore duplicate values
- DENSE_RANK() always skips numbers, RANK() does not
Cumulative Sum Example
Which SQL window function can be used to get a running cumulative sum of sales per month?
- SUM(Sales) OVER (ORDER BY Month)
- COUNT(Sales) GROUP BY Month
- TOTAL SUM SALES
- JOIN Sales ON Month
- SELECT SUM(Sales) FROM SalesTable