SQL Queries

Categories: Code Problem / Data Structures and Algorithms / Recursion (32) Code Problem / Data Structures and Algorithms / Binary Search (30) Code Problem / Data Structures and Algorithms / Linked Lists (26) Code Problem / Data Structures and Algorithms / Backtracking (26) Code Problem / Data Structures and Algorithms / Stacks and Queues (25) Code Problem / Data Structures and Algorithms / Hashing (24) Databases / SQL / Backup and Recovery (19) Code Problem / Data Structures and Algorithms / Arrays and Strings (19) Databases / SQL / Database Normalization (18) Code Problem / Data Structures and Algorithms / Time Complexity (17) Databases / SQL / Locking (16) Databases / SQL / Replication (15) Databases / SQL / SQL Queries (13) Code Problem / Data Structures and Algorithms / Graphs (13) Databases / SQL / Database Security (13)  More...

Recent solutions:

Check these solutions from our community:
Databases / SQL DifficultyMedium SQL
This question asks for the 5th highest salary from the "Employees" table. The example input is a table with id, name, and salary columns, and the example output is a table with a salary column.
About this solution: The solution above is optimal because it uses the LIMIT clause to limit the number of rows returned by the query.
Feb 02
Databases / SQL DifficultyHard SQL
This technical problem deals with finding common records between two SQL tables. An example is given, along with the expected output.
About this solution: The candidate's solution is correct and uses an inner join, which is the most efficient way to find common records between two tables.
Jan 31
Databases / SQL DifficultyHard SQL
This technical problem asks the reader to write a SQL query to find the top 10 customers by total spend. The table of customer data is provided as an example, and the expected output is also provided.
About this solution: The candidate's solution is correct and demonstrates a good understanding of SQL. The solution is optimal, using the ORDER BY and LIMIT clauses to sort the table by total_spend in descending order and then limit the results to the top 10 customers.
Jan 31
Databases / SQL DifficultyHard SQL
This technical problem involves finding all customers who have ordered a product that is out of stock. The input is three tables: customers, orders, and products. The output is the customers table with the id and name of the customers who have ordered a product that is out of stock.
About this solution: The candidate's solution is incomplete - it does not return the expected output. The candidate has correctly identified that a join is needed to combine the data from the three tables, but has not included all the necessary conditions in the WHERE clause. A better approach would be to use a LEFT JOIN and check for NULL values in the products table, as this would return all customers who have placed an order for a product that is out of stock.
Dec 01
Databases / SQL DifficultyHard SQL
Write a SQL query that returns the names of all the people who have a score greater than 80.
About this solution: The candidate's solution is correct and demonstrates a good understanding of SQL.
Nov 26
Databases / SQL DifficultyHard SQL
This technical problem involves finding all customers who have ordered a product that is out of stock. The input is three tables: customers, orders, and products. The output is the customers table with the id and name of the customers who have ordered a product that is out of stock.
About this solution: The solution is correct and demonstrates a level of completeness. It solves the problem and uses the correct approach.
Nov 25
Databases / SQL DifficultyMedium SQL
To find the total number of orders for each customer, you can use a SQL query as follows: SELECT customer_id, COUNT(order_id) AS total_orders FROM customer_orders GROUP BY customer_id;
About this solution: The candidate's solution is correct and uses an optimal approach.
Nov 18
Databases / SQL DifficultyMedium SQL
This technical problem involves writing a SQL query to find employees who make more than the average salary.
About this solution: The solution above is optimal because it uses a subquery to find the average salary of all employees, and then uses that value to filter the employees table.
Nov 13