How to Practice SQL without creating tables in your database

Pradeepchandra Reddy S C
3 min readSep 3, 2023

--

Image

Learning SQL is an essential skill for anyone working with databases or data analysis. Traditionally, practicing SQL involved creating physical tables in your database, which can be cumbersome and risky, especially in a production environment.

However, there’s a better way to hone your SQL skills without the need for physical tables — by using Common Table Expressions (CTEs). In this article, we’ll explore how to practice SQL effectively using CTEs, without creating any permanent data structures in your database.

  1. Identifying the Data Requirements: Before you start practicing SQL with CTEs, it’s crucial to determine the kind of data you need for your practice. Think about the structure and content of the tables you would typically use in your database. Understanding your data requirements will guide you in creating relevant CTEs.
  2. Creating CTEs: Instead of creating physical tables, you can create CTEs to simulate the data you need. CTEs are temporary result sets defined within your SQL query, and they exist only for the duration of that query.

Here’s an example of creating CTEs for employees and departments:

WITH Employees AS (
SELECT 1 AS EmployeeID, 'Rajesh' AS FirstName, 'Kumar' AS LastName, 'Sales' AS Department
UNION ALL
SELECT 2, 'Priya', 'Patel', 'HR'
UNION ALL
SELECT 3, 'Amit', 'Sharma', 'IT'
),
Departments AS (
SELECT 'Sales' AS DepartmentName, 'Mumbai' AS Location
UNION ALL
SELECT 'HR', 'Delhi'
UNION ALL
SELECT 'IT', 'Bangalore'
)
SELECT * FROM Employees;
SELECT * FROM Departments;

3. Writing Queries: With your CTEs in place, you can now write SQL queries just as you would with physical tables. You can join, filter, and manipulate the data within these CTEs. Here’s an example of querying data from the previously created CTEs:

SELECT e.FirstName, e.LastName, d.Location
FROM Employees e
JOIN Departments d ON e.Department = d.DepartmentName;

4. Practice and Experiment: You can run these queries in your SQL environment to practice and experiment with SQL without affecting your actual database tables. This approach provides a safe and flexible way to enhance your SQL skills.

5. Disposal of CTEs: It’s essential to remember that CTEs are temporary and exist only for the duration of the query execution. Once you close the query, the CTEs are automatically removed. You don’t need to worry about cleaning up or dropping these temporary structures, making this method efficient and hassle-free.

Using Common Table Expressions (CTEs) for practice is a powerful technique that allows you to develop your SQL skills without creating physical tables.

By understanding your data requirements, creating relevant CTEs, and writing queries, you can gain hands-on experience and confidence in SQL. So, start practicing with CTEs today and take your SQL skills to the next level.

Ankit Bansal

Happy querying!

I hope this was somehow useful to you, if it is say a thanks on my Linkedin

Thanks for reading! If you like the article make sure to:

  • 👏 Clap for the story (50 claps) and follow me
  • 📰 View more content on my medium profile

🔔 Follow Me On:

LinkedIn | GitHub | Instagram | Topmate

--

--