The Ultimate SQL Guide for Beginners.

SQL is a standard language for storing, manipulating, and retrieving data in databases.

Image for post
Photo by Caspar Camille Rubin on Unsplash

In this article, I’ll teach you the SQL language fundamentals and hope you will write your own database queries at the end.

What does SQL Mean?

SQL stands for Structured Query Language and lets you access and manipulate databases.

Syntax

Most of the actions you need to perform on a database are done with SQL statements. The following SQL statement selects all the records in the “Users” table:

SELECT * FROM Users;

Select

The select statement is used to retrieve data from a database. The requested data is returned in a results table.

SELECT column1 FROM table_name;

Select Distinct

The Select Distinct statement is used to return only distinct (different) values.

SELECT DISTINCT * FROM table_name;

Count

The following SQL statement lists the number of different customer countries:

SELECT COUNT(DISTINCT Country) FROM Customers;

Where

The Where clause is used to filter records.

SELECT column1
FROM table_name
WHERE condition;

For example:

SELECT * FROM Users
WHERE Country='Netherlands';

AND, OR and NOT Operators

The Where clause can be combined with AND, OR, and NOT operators. The AND and OR operators are used to filter records based on more than one condition:

  • The AND operator displays a record if all the conditions separated by AND are TRUE.
  • The OR operator displays a record if any of the conditions separated by OR is TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

AND

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

OR

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

NOT

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

Order By

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

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

The following SQL statement selects all users from the “Users” table, sorted DESCENDING by the “Country” column:

SELECT * FROM Users
ORDER BY Country DESC;

Insert

The Insert statement is used to insert new records in a table. It is possible to write the Insert statement in two ways.

First way

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

Second way

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

Update

The Update statement is used to modify the existing records in a table.

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

Delete

The Delete statement is used to delete existing records in a table.

DELETE FROM table_name WHERE condition;

Joins

A Join clause combines rows from two or more tables, based on a related column.

Different Types of SQL Joins

Here are the different types of the Joins in SQL:

  • (INNER) JOIN: Returns records that have matching values in both tables
  • LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
  • RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table
  • FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table

Inner Join

The Inner Join keyword selects records that have matching values in both tables.

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

Left Join

The Left Join keyword returns all records from the left table (table1), and the matched records from the right table (table2).

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Right Join

The Right Join keyword returns all records from the right table (table2), and the matched records from the left table (table1).

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

Full Join

The Full Outer Join keyword returns all records when there are a match in left (table1) or right (table2) table records.

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

Group

The group by statement groups rows with the same values into summary rows, like “find the number of users in each country.”

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

Conclusion

After this article, I hope you have learned the fundamentals of SQL, and you can write your own creative queries to manage your database!

Pursuing my Bachelor in Computer Science. Dutch parliament ambitions. https://bryandijkhuizen.com/ — Signup for my newsletter! https://bit.ly/3scKwKw

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store