17th Oct 2023 9 minutes read How to Create Your Own Database to Practice SQL Nicole Darnley sql practice learn sql sql queries Table of Contents Why You Need Your Own Database for Practice A Step-by-Step Guide to Setting up Your SQL Database Step 1: Install Required Software Step 2: Create a SQL Database Step 3: Create Custom Tables Step 4: Import Data From CSVs Practice Queries Example 1: What is the distinct count of customers? Example 2: What is the average number of invoices per customer? The Long-Term Benefits of Regular Practice Practice SQL on Your Own Database! Welcome to the step-by-step guide to creating your own SQL database from scratch, designed with beginners in mind. This guide not only helps you set up your database but also introduces you to essential SQL practice exercises to get you started on your learning journey. Follow along to gain SQL hands-on practice and foster a deeper understanding of SQL functionalities. Set a solid foundation for more advanced SQL training! Everyone has a different style of learning, but one thing we all need is SQL hands-on practice. It is one thing to read about SELECT statements, JOINS, and GROUP BY clauses, but it is an entirely different experience to use them in queries and see the results. This is a lot like when I took 3 years of Spanish in school. One thing I found both fascinating and frustrating was that I could understand the teacher but struggled to form sentences to respond. This is not unusual; it often happens because we practice learning new words but neglect using them. Learning by doing is one of the most effective ways to acquire a new skill. So, what is the most effective way to practice “speaking” SQL? That is our focus in this article. If you’re interested in more on the best ways to learn SQL, be sure to check out this article here. LearnSQL.com offers several comprehensive courses that include interactive exercises for SQL practice. You go through hundreds of exercises, covering dozens of hours to help you master SQL. For example, the SQL Basics course includes 129 practice exercises and 10 hours of hands-on learning. In addition, the course Data Types in SQL includes 89 exercises and another 10 hours of instruction. These courses are a great way to learn the “new words” of SQL, and the practice exercises help you use them in writing your queries. However, while structured learning is a great resource, you can take your learning to a whole new level by setting up your own SQL database for practice. In this article, I will walk you through the tools and steps you need to accomplish this task. Why You Need Your Own Database for Practice When you have your own database, you get the freedom to customize it however you want, from the table structure to the actual data it houses. You can create real-world scenarios tailored to your learning objectives. Your data stays with you, and you may explore and even make mistakes, without any judgment or fear of breaking something. Plus, the ability to stimulate real-world scenarios allows you to practice SQL for job-specific conditions, making you better prepared for the professional world. Looking for a job in e-commerce? Set up your database with an order table, a customer table, and a product table. Interested in a career in finance? Great! Create a table for accounts receivable and another for accounts payable. The flexibility in creating your own database is unmatched by other ways to practice SQL. If setting up a database sounds intimidating to you, don’t worry! It is very simple in practice. Different databases have different nuances. We won’t spend time here explaining them, but you can learn more about some of the most popular databases in 2023 in this article. A Step-by-Step Guide to Setting up Your SQL Database Step 1: Install Required Software OK, let’s get started setting up your database for SQL training. The first thing you need to do is to download a database tool. One that I use that works on Windows, Linux, and Mac is DBeaver, which may be downloaded here. Best of all, it’s free! Want some other tool? Check the article on the best SQL IDEs. Once you’ve downloaded and installed the software, launch the application. You should see a screen that looks like this: Step 2: Create a SQL Database In the top toolbar, click on “Help” and then “Create Sample Database.” You then see a prompt asking if you would like to create a sample database. Click “Yes.” You should now see a sample database listed under your connections under the Database Navigator panel. This sample database comes with already created tables and views you can query immediately. Later in this article, there are a few examples of SQL queries for practice with your new database. For now, let’s look at how we may further customize this database. Step 3: Create Custom Tables Let’s go back to our example of practicing with e-commerce data. I’m going to rename my database to ecommerce_data by right-clicking on the database name and selecting “rename.” To create a new table for orders, we execute a CREATE TABLE statement. In this statement, we define the table name of orders and then define the column names and the data types. CREATE TABLE orders ( order_id varchar(255), customer_id varchar(255), order_date date, price decimal, quantity int, shipping decimal, tax decimal, total_order_amount decimal ) You can now run SELECT * FROM orders, but you will see an empty table. The next step is to insert rows into this table using an INSERT statement. INSERT into orders VALUES ('12345','654321', '2023-07-01', 20.00, 1, 3.99, 1.75, 25.74) Now, you will see your new data when you run a SELECT statement. Step 4: Import Data From CSVs Inserting individual rows of data is tedious depending on how much data you want to set up in your database. Another option is to import whole CSV files rather than writing INSERT statements. To do this, right-click on the table name under your sample database and select “Import Data.” From here, select a CSV from your computer to upload. If you do not see your table listed under your sample database, right-click on the database name and click “Refresh.” You should then see the newly created tables. There you have it! Your own database to practice SQL. If you’re unsure about creating your own data, you can also find several resources online with public datasets for download. You can then import these files into your database. A good, clean data set often used is the superstore data from Tableau. This Excel sheet includes 3 separate tabs to create 3 tables of store purchase data. Another great resource for finding data sets is our article, “Where Can I Find Free Online Data Sets to Practice SQL?.” Practice Queries Let’s walk through a few examples to get you started in using your new database. This is only the beginning; you’ll be on to exploring your data in no time. For an added challenge, try answering the question on your own before reading the SQL query. The questions below use the customer and invoice tables in the DBeaver sample database. Example 1: What is the distinct count of customers? For this example, we only need to query the customer table. Use COUNT() and DISTINCT to answer this question. select count(distinct CustomerId) from customer This query counts the number of unique CustomerIds in the customer table. We now see there are 59 customers in our database. Next, let’s see how many invoices each customer has on average. Example 2: What is the average number of invoices per customer? To answer this question, use the invoice table and create a list of all customer IDs with how many invoices each has. select CustomerId, count(distinct invoiceId) as invoices from invoice group by 1 At a glance, it looks like most customers have 6 or 7 invoices. Let’s calculate the average across all customer IDs by placing the above in a subquery and calculating the average: select avg(invoices) from ( select CustomerId, count(distinct invoiceId) as invoices from invoice group by 1 ) The query returns 6.98 invoices, on average, for each customer. These examples are just to get you started. Get familiar with what data lives in each table and how the tables relate to one another. Then, begin asking yourself questions and creating hypotheses. Finally, begin exploring the data by writing queries to answer those questions and test your hypotheses. A great resource to reference is our SQL for Data Analysis Cheat Sheet. This is a curated list of SQL commands to help you get started. Once you get started, it’ll be hard to stop! The Long-Term Benefits of Regular Practice There are numerous advantages to practicing SQL regularly. You improve your problem-solving skills, make yourself more marketable for higher-paying jobs, and get better equipped to handle complex databases. Trust me, practice does make perfect, and the more real-world problems you solve, the better you become. Regular SQL practice significantly enhances your analytical thinking. As you delve deeper into your SQL database setup and start working with more complex queries, you learn to analyze data from various angles. This not only helps in extracting precise information but also in understanding the intricate relationships between different data sets. Moreover, the ability to dissect and interpret complex data is a highly valued skill in many professions today. This puts you a step ahead in your career journey. As you practice SQL through hands-on exercises, you find yourself becoming more autonomous and confident in your abilities. You learn to trust your judgment and develop a knack for finding solutions to complex problems. This is an indispensable trait in the ever-evolving tech landscape. Furthermore, by setting up your own SQL database for practice, you foster a deep understanding of the database structure and nuances. This is a substantial asset in both learning and professional environments. Dedicating time to SQL practice exercises is an investment in your future. The world is becoming increasingly data-driven. Proficiency in SQL is often a prerequisite in many fields, not just in IT but also in marketing, finance, healthcare, and more. By committing to regular practice, you are not just learning a skill but are paving the path to opportunities in a wide array of industries, opening doors to potential roles that are both fulfilling and financially rewarding. Practice SQL on Your Own Database! So, there you have it! Your own SQL database setup for practice is not only possible but incredibly beneficial. You get customization, privacy, and the ability to simulate real-world scenarios. If you’re looking to upskill further, remember to check out the extensive courses offered by LearnSQL.com. The All Forever package provides lifetime access to all the courses and tracks plus any new courses released in the future! This option, by far, gives you the most bang for your buck with a huge discount. Start your SQL journey now by setting up your personal database. Dive into it, practice daily, and you’ll be amazed at how far you go. Happy querying! Tags: sql practice learn sql sql queries