Learn SQL: Beginner's Guide to SQL

Beginner's guide to SQL covers basics of data manipulation and joining tables. Start exploring SQL for data analysis

By

Jatin Solanki

Updated on

November 14, 2024

Learn SQL: A Beginner's Guide to SQL

SQL, or Structured Query Language, is a programming language used to manage and manipulate data stored in relational databases. SQL is an essential tool for data analysts, data scientists, and software developers who work with large amounts of data. In this beginner's guide, we will walk you through the basics of SQL, including how to create, retrieve, update, and delete data, as well as join tables together.


Getting Started with SQL

Before you start writing SQL queries, you need to set up a database. There are several relational database management systems (RDBMS) that you can use, including MySQL, Oracle, and Microsoft SQL Server. You can install these RDBMS on your local machine or use a cloud-based service like Amazon Web Services (AWS) or Microsoft Azure.

Creating a Database

To create a database in MySQL, you can use the following SQL command:

This command will create a database named my_database. Once you have created a database, you can create tables to store your data.

Creating a Table

To create a table in MySQL, you can use the following SQL command:

This command will create a table named my_table with three columns: id, name, and age. The id column is set as the primary key, which means it will have a unique value for each row in the table.

Retrieving Data with SQL

Once you have created a database and a table, you can retrieve data from the table using the SELECT statement. Here's an example:

This command will retrieve all the data from the my_table table. If you only want to retrieve specific columns, you can list them after the SELECT keyword:

Updating Data with SQL

You can update existing data in a table using the UPDATE statement. Here's an example:

This command will update the age column for the row with the name John to 30. If you want to update multiple columns, you can separate them with commas:

Deleting Data with SQL

You can delete data from a table using the DELETE statement. Here's an example:

This command will delete all the rows from the my_table table where the name is John. If you want to delete all the rows in the table, you can omit the WHERE clause:

Joining Tables with SQL

One of the most powerful features of SQL is the ability to join tables together. Joins allow you to combine data from multiple tables based on a common column. There are several types of joins in SQL, including inner joins, left joins, right joins, and full outer joins.

Inner Joins

Inner joins return only the rows from both tables where the join condition is true. Here's an example:

This command will join the my_table table with the my_other_table table on the id column and return only the rows where there is a match between the two tables.

Left Joins

Left joins return all the rows from the left table and the matching rows from the right table. If there is no match in the right table, the result will contain null values for the columns from the right table. Here's an example:

This command will return all the rows from the my_table table and the matching rows from the my_other_table table. If there is no match in the my_other_table, the result will contain null values for the columns from my_other_table.

Right Joins

Right joins are similar to left joins, but they return all the rows from the right table and the matching rows from the left table. If there is no match in the left table, the result will contain null values for the columns from the left table. Here's an example:

This command will return all the rows from the my_other_table table and the matching rows from the my_table table. If there is no match in the my_table, the result will contain null values for the columns from my_table.

Full Outer Joins

Full outer joins return all the rows from both tables and match them based on the join condition. If there is no match in one of the tables, the result will contain null values for the columns from the table without a match. Here's an example:

This command will return all the rows from both the my_table table and the my_other_table table, and match them based on the id column.

Conclusion

SQL is a powerful tool for managing and manipulating data stored in relational databases. In this beginner's guide, we covered the basics of SQL, including how to create a database and a table, retrieve data, update data, delete data, and join tables together. With these skills, you can start exploring the vast world of SQL and using it to analyze and manipulate data.

External Links

Table of Contents

Read other blog articles

Grow with our latest insights

Sneak peek from the data world.

Thank you! Your submission has been received!
Talk to a designer

All in one place

Comprehensive and centralized solution for data governance, and observability.

decube all in one image