Test your knowledge of SQL fundamentals with these 15 easy questions covering common SQL interview topics such as JOINs, ranking functions, filtering, and aggregation. Ideal for candidates preparing for SQL interviews and anyone wanting to boost their SQL basics.
Which SQL clause would you use to find duplicate values in a column called 'email' within a table 'users'?
Explanation: Using GROUP BY groups the rows by email and HAVING COUNT(email) u003E 1 filters only those with duplicates. WHERE COUNT(email) u003E 1 is incorrect because WHERE cannot use aggregate functions this way; that's what HAVING is for. ORDER BY just sorts the emails, and DISTINCT email removes duplicates but does not identify them.
What query can you use to retrieve the second highest salary from an 'employees' table?
Explanation: To find the second highest salary, you filter out the highest and use MAX on the rest. The DESC LIMIT 1 syntax returns the top salary, not the second highest. MIN finds the lowest salary. Using WHERE salary = (SELECT MIN(salary)) gets the minimum salary, not the second highest.
How does the RANK() window function behave when there are ties in the partitioned column?
Explanation: RANK() gives identical ranks to tied values, but skips the next rank(s). DENSE_RANK() does not skip ranks. Assigning different ranks to ties or ranking sequentially without skipping is incorrect for RANK(). Tied rows are included in the results; they are not ignored.
What is the key difference between the WHERE and HAVING clauses in SQL?
Explanation: WHERE is used to filter individual rows before any groupings or aggregations, while HAVING filters groups after aggregation. The second option is wrong because WHERE is not exclusive to GROUP BY. The third option reverses their true behavior. WHERE and HAVING are not interchangeable in all scenarios.
Which SQL window function helps you calculate a moving average over a column 'price' ordered by 'date'?
Explanation: The AVG window function with an OVER clause and window frame specification calculates the moving average. COUNT only gives the number of rows. MAX returns the highest price, not an average. SUM with GROUP BY does sums per date, not a moving average.
Which keyword removes duplicate rows from the result set in a basic SELECT query?
Explanation: DISTINCT is the correct syntax to remove duplicates in SQL. UNIQUEE and DISTINC are common typos. DIFFERENT is not a SQL keyword; it has no effect in a query.
If you want to get all records that exist in both table A and table B, which type of JOIN should you use?
Explanation: INNER JOIN returns only rows with matching values in both tables. LEFT JOIN and RIGHT JOIN return all records from one side and matches from the other, including non-matching rows. OUTER JOIN isn't valid SQL syntax; the proper term is FULL OUTER JOIN, which includes all records from both tables.
How would you select all rows where the 'end_date' column is missing a value in SQL?
Explanation: IS NULL checks for missing values. Using = NULL doesn't work in SQL. ISNULL is a function in some SQL dialects but is not valid in WHERE clauses this way. WHERE end_date NOT NULL is incorrect syntax.
Which clause in SQL limits the number of rows returned by a SELECT statement?
Explanation: LIMIT restricts the number of rows outputted by a query. RESTRICT and SHORT are not SQL keywords. COUNT calculates the number of rows but doesn't limit returned results.
How do you sort the results by 'created_at' in descending order in a SQL query?
Explanation: ORDER BY column DESC is the proper way to sort in descending order. The other options are invalid SQL syntax, either missing the BY keyword or using incorrect commands.
Which aggregate function would you use to count the total number of users in a 'users' table?
Explanation: COUNT(*) counts all rows, giving the total number of users. SUM is used for addition, not counting. AVG gives the average but does not count. COUNTT is a typo and not a valid function.
How can you retrieve a list of all unique cities from a 'customers' table?
Explanation: SELECT DISTINCT retrieves unique city values. UNIQUE and DIFF are not SQL keywords. GROUPED is incorrect syntax and will not return unique cities.
If you want to count how many orders each user has made, which clause should you use?
Explanation: GROUP BY user_id groups the rows so you can aggregate order counts per user. ORDER BY only sorts. LIMIT restricts output; HAVING user_id u003E 5 filters groups but does not count them.
Which command retrieves all columns from the 'products' table?
Explanation: SELECT * FROM products is the correct command for returning all columns. RETRIEVE and GET ALL are not recognized SQL commands. SELECT ALL products is not valid SQL syntax.
Which syntax assigns a temporary name 'total_orders' to a sum of orders in a result?
Explanation: AS assigns an alias in SQL. TO, =, and NAMED are not valid SQL aliasing syntax. The correct method is to use AS between the calculated field and the alias.
Which clause is used to filter results with more than one condition, for example, age u003E 25 and city = 'Miami'?
Explanation: The WHERE clause with AND allows filtering on multiple conditions. LIMIT and FILTER are not used this way. OR returns rows where either condition is true, not both.