SQL Exercises

Welcome to the SQL Exercises page by The Coding College! Here, you’ll find a collection of carefully crafted SQL exercises designed to help you practice and strengthen your SQL skills. These exercises cover various topics, from basic SQL queries to advanced concepts like joins, subqueries, and aggregate functions.

Why Practice SQL Exercises?

Practicing SQL exercises helps you:

  1. Understand Core Concepts: Reinforce theoretical knowledge with practical application.
  2. Solve Real-World Problems: Learn how SQL is used in data analysis and database management.
  3. Prepare for Interviews: Build confidence in answering SQL-related interview questions.
  4. Develop Problem-Solving Skills: Enhance your ability to write efficient and optimized queries.

SQL Exercises

1. Basic SQL Queries

Exercise 1: Select All Records

Write a query to retrieve all records from the Employees table.

Expected Output: All rows and columns from the Employees table.

Exercise 2: Filter with WHERE Clause

Find all employees whose department is ‘Sales’.

Expected Output: A list of employees working in the ‘Sales’ department.

2. Aggregation and Grouping

Exercise 3: Count Records

Write a query to count the total number of employees in the Employees table.

Expected Output: A single number representing the total count.

Exercise 4: Group and Aggregate

Find the total salary for each department in the Employees table.

Expected Output: A list of departments with their total salaries.

3. SQL Joins

Exercise 5: Inner Join

Retrieve the names of all customers along with the orders they placed. Use the Customers and Orders tables.

Expected Output: A list of customer names and their respective orders.

Exercise 6: Left Join

List all customers, including those who haven’t placed an order.

Expected Output: All customers with corresponding order details, if any.

4. Advanced Queries

Exercise 7: Subquery

Find the names of employees who earn more than the average salary in the Employees table.

Expected Output: Names of employees whose salaries exceed the average.

Exercise 8: Using CASE

Write a query to categorize employees into salary brackets:

  • Low: Salary < 3000
  • Medium: 3000 ≤ Salary ≤ 7000
  • High: Salary > 7000

Expected Output: A table with employee names and their salary brackets.

Tips for Practicing SQL

  1. Start Simple: Begin with basic queries before moving to complex ones.
  2. Use Online Editors: Practice directly in tools like SQL Fiddle or DB Fiddle.
  3. Experiment: Modify sample queries to see how changes affect results.
  4. Challenge Yourself: Try writing queries without referencing examples.

Conclusion

Practicing SQL exercises is an excellent way to improve your query-writing skills and deepen your understanding of database management. Explore more exercises and tutorials on The Coding College to become a proficient SQL user.

Leave a Comment