30 SQL Basic Exercises for Beginners with Solutions
Master beginner SQL skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in SQL, setting a solid foundation for intermediate challenges. Start your journey to SQL mastery today!
Learning Objectives:
By the end of these exercises, you will have a strong understanding of fundamental SQL concepts, including SELECT statements, basic filtering, sorting, and simple joins.
Exercise Instructions:
Start with the first exercise and attempt to solve it before checking the hint or solution.
Ensure you understand the logic behind each solution, as this will help you in more complex problems.
Use these exercises to reinforce your learning and identify areas that may require further study.
1. Retrieve all columns from the Employees table.
Code In Sql
-- Table Name : Employees
-- ID | Name | Department | Salary | Joining_Date
-- ----+---------+------------+--------+--------------
-- 1 | Rajesh | IT | 50000 | 2020-01-10
-- 2 | Sneha | HR | 45000 | 2019-04-12
-- 3 | Kiran | Finance | 52000 | 2021-08-23
-- 4 | Priya | Marketing | 48000 | 2018-06-15
-- 5 | Arjun | IT | 53000 | 2022-09-19
-- Your Query Here
Output
Click Run Button to view compiled output
2. Select only the Name and Salary of each employee.
Code In Sql
-- Table Name : Employees
-- ID | Name | Department | Salary | Joining_Date
-- ----+---------+------------+--------+--------------
-- 1 | Rajesh | IT | 50000 | 2020-01-10
-- 2 | Sneha | HR | 45000 | 2019-04-12
-- 3 | Kiran | Finance | 52000 | 2021-08-23
-- 4 | Priya | Marketing | 48000 | 2018-06-15
-- 5 | Arjun | IT | 53000 | 2022-09-19
-- Your Query Here
Output
Click Run Button to view compiled output
3. Find employees who work in the IT department.
Code In Sql
-- Table Name : Employees
-- ID | Name | Department | Salary | Joining_Date
-- ----+---------+------------+--------+--------------
-- 1 | Rajesh | IT | 50000 | 2020-01-10
-- 2 | Sneha | HR | 45000 | 2019-04-12
-- 3 | Kiran | Finance | 52000 | 2021-08-23
-- 4 | Priya | Marketing | 48000 | 2018-06-15
-- 5 | Arjun | IT | 53000 | 2022-09-19
-- Your Query Here
Output
Click Run Button to view compiled output
4. Retrieve employees with a Salary greater than 50000.
Code In Sql
-- Table Name : Employees
-- ID | Name | Department | Salary | Joining_Date
-- ----+---------+------------+--------+--------------
-- 1 | Rajesh | IT | 50000 | 2020-01-10
-- 2 | Sneha | HR | 45000 | 2019-04-12
-- 3 | Kiran | Finance | 52000 | 2021-08-23
-- 4 | Priya | Marketing | 48000 | 2018-06-15
-- 5 | Arjun | IT | 53000 | 2022-09-19
-- Your Query Here
Output
Click Run Button to view compiled output
5. Display employee names in alphabetical order.
Code In Sql
-- Table Name : Employees
-- ID | Name | Department | Salary | Joining_Date
-- ----+---------+------------+--------+--------------
-- 1 | Rajesh | IT | 50000 | 2020-01-10
-- 2 | Sneha | HR | 45000 | 2019-04-12
-- 3 | Kiran | Finance | 52000 | 2021-08-23
-- 4 | Priya | Marketing | 48000 | 2018-06-15
-- 5 | Arjun | IT | 53000 | 2022-09-19
-- Your Query Here
Output
Click Run Button to view compiled output
6. Count the total number of employees in the Employees table.
Code In Sql
-- Table Name : Employees
-- ID | Name | Department | Salary | Joining_Date
-- ----+---------+------------+--------+--------------
-- 1 | Rajesh | IT | 50000 | 2020-01-10
-- 2 | Sneha | HR | 45000 | 2019-04-12
-- 3 | Kiran | Finance | 52000 | 2021-08-23
-- 4 | Priya | Marketing | 48000 | 2018-06-15
-- 5 | Arjun | IT | 53000 | 2022-09-19
-- Your Query Here
Output
Click Run Button to view compiled output
7. Find employees who joined before 2021.
Code In Sql
-- Table Name : Employees
-- ID | Name | Department | Salary | Joining_Date
-- ----+---------+------------+--------+--------------
-- 1 | Rajesh | IT | 50000 | 2020-01-10
-- 2 | Sneha | HR | 45000 | 2019-04-12
-- 3 | Kiran | Finance | 52000 | 2021-08-23
-- 4 | Priya | Marketing | 48000 | 2018-06-15
-- 5 | Arjun | IT | 53000 | 2022-09-19
-- Your Query Here
Output
Click Run Button to view compiled output
8. Show distinct departments in the Employees table.
Code In Sql
-- Table Name : Employees
-- ID | Name | Department | Salary | Joining_Date
-- ----+---------+------------+--------+--------------
-- 1 | Rajesh | IT | 50000 | 2020-01-10
-- 2 | Sneha | HR | 45000 | 2019-04-12
-- 3 | Kiran | Finance | 52000 | 2021-08-23
-- 4 | Priya | Marketing | 48000 | 2018-06-15
-- 5 | Arjun | IT | 53000 | 2022-09-19
-- Your Query Here
Output
Click Run Button to view compiled output
9. Retrieve the top 3 highest-paid employees.
Code In Sql
-- Table Name : Employees
-- ID | Name | Department | Salary | Joining_Date
-- ----+---------+------------+--------+--------------
-- 1 | Rajesh | IT | 50000 | 2020-01-10
-- 2 | Sneha | HR | 45000 | 2019-04-12
-- 3 | Kiran | Finance | 52000 | 2021-08-23
-- 4 | Priya | Marketing | 48000 | 2018-06-15
-- 5 | Arjun | IT | 53000 | 2022-09-19
-- Your Query Here
Output
Click Run Button to view compiled output
10. Calculate the average salary of employees.
Code In Sql
-- Table Name : Employees
-- ID | Name | Department | Salary | Joining_Date
-- ----+---------+------------+--------+--------------
-- 1 | Rajesh | IT | 50000 | 2020-01-10
-- 2 | Sneha | HR | 45000 | 2019-04-12
-- 3 | Kiran | Finance | 52000 | 2021-08-23
-- 4 | Priya | Marketing | 48000 | 2018-06-15
-- 5 | Arjun | IT | 53000 | 2022-09-19
-- Your Query Here