SQL 101 - Basics to Learn [2023]

Learn SQL language from scratch.

SQL (Structured Query Language) is a programming language used to manage and manipulate data in relational databases. It is widely used in the technology industry to retrieve, insert, update, and delete data from databases. SQL is an essential skill for data analysts, software engineers, and web developers who work with data. This article is a SQL 101 guide that covers the basics of SQL, resources, examples, and next steps.

Learning SQL can provide several advantages for individuals in the tech industry. Some of the key advantages include:

Improved job prospects: SQL is widely used across different industries and job roles, and there is a high demand for professionals who can work with databases and manipulate data. Learning SQL can open up new job opportunities, including roles such as data analyst, database administrator, business intelligence analyst, and software developer.

Increased productivity: SQL can help you work more efficiently with data, as it provides a powerful set of tools for querying, filtering, and manipulating data. By learning SQL, you can streamline your data analysis workflows and work more effectively with large datasets.

Better decision-making: SQL can help you derive insights from data, which can inform decision-making and help you make better-informed business decisions. By learning SQL, you can work with data in a more informed and systematic way, which can lead to more accurate and reliable insights.

Improved collaboration: SQL is a widely used language in the tech industry, and many teams use it to collaborate on data-related projects. By learning SQL, you can better communicate with colleagues and collaborate on data-related tasks, which can improve team productivity and project outcomes.

There are several types of jobs that require SQL skills. Here are a few examples:

Data analyst: Data analysts use SQL to extract and analyze data from databases, develop reports and dashboards, and identify trends and patterns in data. They work in a variety of industries, including finance, healthcare, and technology.

Database administrator: Database administrators use SQL to manage databases, including installing and configuring database software, creating and maintaining database structures, and managing user access and permissions.

Business intelligence analyst: Business intelligence analysts use SQL to develop reports and dashboards that provide insights into business performance, identify trends and patterns in data, and inform decision-making.

Software developer: Software developers use SQL to develop and maintain database-driven applications, including web applications, mobile applications, and enterprise software.

Data scientist: Data scientists use SQL to extract and analyze data, develop predictive models, and communicate insights to stakeholders. They work in a variety of industries, including healthcare, finance, and technology.

Learning SQL can provide many advantages for individuals in the tech industry, including improved job prospects, increased productivity, better decision-making, and improved collaboration. There are several types of jobs that require SQL skills, including data analyst, database administrator, business intelligence analyst, software developer, and data scientist.

Getting Started with SQL

To get started with SQL, you need to install a database management system (DBMS) that supports SQL. There are several popular DBMSs that support SQL, such as MySQL, Oracle, Microsoft SQL Server, and PostgreSQL. Once you have installed a DBMS, you can start using SQL to interact with the database.

SQL Basics

Here are some SQL basics that you should keep in mind when working with SQL:

Always use semicolons at the end of each SQL statement. Use single quotes to enclose string values. SQL is not case-sensitive, but it is a good practice to write SQL keywords in uppercase to make the code more readable. 4. Always back up your database before making any changes to it.

Use aliases to make your SQL queries more readable. Use comments to document your SQL code.

SQL Syntax

SQL has a simple syntax that is easy to understand. It consists of a set of commands that are used to interact with the database. The basic commands in SQL are:

SELECT – used to retrieve data from the database

INSERT – used to insert data into the database

UPDATE – used to update data in the database

DELETE – used to delete data from the database

CREATE – used to create tables in the database

ALTER – used to modify tables in the database

DROP – used to delete tables from the database

SQL commands are written in uppercase, but the actual data is not case-sensitive.

Use the SELECT statement to retrieve data from a database. The basic syntax is:


SQL Examples

Let's take a look at some SQL examples to get a better understanding of how SQL works.

SELECT Statement

The SELECT statement is used to retrieve data from the database. Here is an example of a SELECT statement:

SELECT * FROM customers;

This query retrieves all the data from the customers table.

INSERT Statement

The INSERT statement is used to insert data into the database. Here is an example of an INSERT statement:

INSERT INTO customers (first_name, last_name, email) VALUES ('John', 'Doe', 'john@example.com');

This query inserts a new record into the customers table.

UPDATE Statement

The UPDATE statement is used to update data in the database. Here is an example of an UPDATE statement:

UPDATE customers SET email='john.doe@example.com' WHERE customer_id=1;

This query updates the email address of the customer with ID 1 in the customers table.

DELETE Statement

The DELETE statement is used to delete data from the database. Here is an example of a DELETE statement:

DELETE FROM customers WHERE customer_id=1;

This query deletes the customer with ID 1 from the customers table.

Use the SELECT statement to retrieve data from a database. The basic syntax is:

SELECT column1, column2, ... FROM table_name;

Use the WHERE clause to filter the data based on a condition. The basic syntax is:

SELECT column1, column2, ... FROM table_name WHERE condition;

Use the ORDER BY clause to sort the data in ascending or descending order. The basic syntax is:

SELECT column1, column2, ... FROM table_name ORDER BY column1 ASC|DESC;

Use the GROUP BY clause to group the data based on one or more columns. The basic syntax is:

SELECT column1, column2, ... FROM table_name GROUP BY column1, column2, ...;

Use the INSERT INTO statement to insert new data into a table. The basic syntax is:

INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

Use the UPDATE statement to modify existing data in a table. The basic syntax is:

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

Use the DELETE statement to delete data from a table. The basic syntax is:

DELETE FROM table_name WHERE condition;

Use the LIKE operator to search for data that matches a specific pattern. The basic syntax is:

SELECT column1, column2, ... FROM table_name WHERE column1 LIKE pattern;

Example of using INNER JOIN to combine data from two tables based on a related column:

Suppose you have two tables: "employees" and "departments". The "employees" table contains information about each employee, including their name, salary, and department ID. The "departments" table contains information about each department, including its name and ID.

To combine the data from these two tables based on the department ID, you would use INNER JOIN like this:

SELECT employees.name, employees.salary, departments.name as department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id;

In this example, the "employees" table and the "departments" table are joined on the "department_id" column in the "employees" table and the "id" column in the "departments" table. The SELECT statement selects the employee name, salary, and department name from the two tables, and uses the "AS" keyword to rename the "name" column in the "departments" table to "department_name" for clarity.

INNER JOIN returns only the rows from both tables that have matching values in the specified column(s). So, in this case, only the employees who belong to a department and the departments that have at least one employee are returned.

SQL Resources

There are several resources available online to learn SQL. Here are some of the best resources to get started:

W3Schools SQL Tutorial – W3Schools is one of the best resources for learning web development technologies. Their SQL tutorial is a comprehensive resource that covers all the basics of SQL.

SQLZoo – SQLZoo is an interactive tutorial that allows you to practice SQL queries in a browser-based environment. It has a set of challenges that start from the basics and move to more advanced topics.

Codecademy – Codecademy is an online learning platform that offers interactive coding courses. Their SQL course is a great resource for beginners who want to learn SQL.

Udemy – Udemy is an online learning platform that offers courses on various topics. They have several SQL courses that are suitable for beginners and advanced learners.

Next Steps

Once you have a basic understanding of SQL, you can start exploring more advanced topics. Here are some next steps to consider:

Explore build something – build a simple database and start writing SQL queries to interact with it.

Explore aggregate functions – aggregate functions are used to perform calculations on data, such as calculating the average or sum of a set of values.

Practice writing complex queries – as you become more comfortable with SQL, challenge yourself to write more complex queries that involve multiple tables and subqueries.

Learn about database design – understanding database design principles will help you create efficient and effective database structures.