SQL Lesson 18: Inserting Sample Data and Running Queries

Abdülbaki Yaşat
2 min readOct 24, 2023

--

photo from sqlshack

Introduction

If you’re a Microsoft SQL Server user and want to learn how to insert sample data into your database and perform queries, you’ve come to the right place. In this guide, we’ll walk you through the process step by step.

Inserting Sample Data

Before you can start querying your database, you need to have data to work with. For this example, we’ll use an Excel file containing sample data. Here’s how you can insert this data into your Microsoft SQL Server database:

  1. Prepare Your Data: Open the Excel file with your sample data. Ensure that the tables you want to populate in your SQL Server database are empty.
  2. Copy the Data: Select the sample data in Excel and copy it to your clipboard. You can do this by using the keyboard shortcut (Ctrl+C on Windows or Command+C on Mac) or by right-clicking and selecting “Copy.”
  3. SQL Server Management Studio: Open SQL Server Management Studio, which is your SQL interface for SQL Server.
  4. Write SQL Statements: Write SQL statements to insert the data into the corresponding table. Here’s a sample SQL statement to insert data into a “countries” table:
INSERT INTO countries (column1, column2, column3, ...) 
VALUES (value1, value2, value3, ...);

Replace “countries” with the name of your table and specify the columns and values accordingly.

5. Execute the SQL Statement: Run the SQL statement to insert the data into the table.

Querying the Data

Now that you’ve successfully inserted sample data into your Microsoft SQL Server database, let’s dive into querying that data. Here’s how you can retrieve information from your database using SQL:

  1. SQL Query: Write SQL queries to extract the data you need. For example, to retrieve all rows and columns from the “countries” table, you can use the following SQL query:
SELECT * FROM countries;

This query will return all the data in the “countries” table. Customize your queries to extract specific information based on your needs.

2. Adapt the Query: Remember to replace “countries” with the actual table name and adapt the SQL queries as needed to fetch the specific data you want.

Conclusion

Congratulations! You’ve successfully inserted sample data into your Microsoft SQL Server database and learned how to query that data using SQL. This skill is crucial for data analysis, reporting, and making informed decisions based on your database’s contents.

Feel free to explore and experiment with your data by crafting more complex SQL queries to extract exactly what you need. You’re now equipped with the knowledge to manage and utilize your Microsoft SQL Server database effectively.

If you have any questions or need further assistance, don’t hesitate to reach out. Happy data querying!

--

--