SQL For Everyone
SQL For Everyone
Table o f Contents
1. Introduction to SQL
2. Basic SQL Syntax
3. Querying Data
4. Filtering and Sorting Data
5. Joining Tables
6. Aggregation Functions
7. Subqueries and Nested Queries
8. Modifying Database Information
9. Advanced SQL Techniques
10. Optimization and Performance Tuning
1. Introduction to SQL
SQL is a standard language designed for managing data in relational
databases. It's commonly used to query, insert, update, and modify
data. Most RDBMS (Relational Database Management System) like MySQL,
SQLite, Oracle, and PostgreSQL use SQL.
As a data analyst, you'll often work with large volumes of data stored
in these databases. SQL becomes an essential tool to retrieve,
manipulate, and analyze this data.
1. RDBMS and Ta b l e s
The SELECT statement is used to select data from a database, and the
FROM statement specifies which table to get the data from.
This query retrieves all first and last names from the Employees table.
2.2 WHERE
SELECT *
FROM Employees
WHERE Position = 'Analyst';
This query retrieves all data for employees who are analysts.
GROUP BY groups rows that have the same values in specified columns
into aggregated data. HAVING is used instead of WHERE with aggregated
data.
SELECT Position, COUNT(*)
FROM Employees
GROUP BY Position
HAVING COUNT(*) > 1;
2.4 ORDER BY
SELECT *
FROM Employees
ORDER BY LastName ASC;
3. Querying Data
The SELECT statement is not just for selecting simple rows. We can use
it to perform calculations, concatenations, and more.
This query concatenates the first and last names, separated by a space,
and displays it as FullName.
SELECT *
FROM Orders
WHERE OrderDate BETWEEN '2023-01-01' AND '2023-12-31';
4.2 L I K E and I L I K E
SELECT *
FROM Employees
WHERE FirstName LIKE 'J%';
This query selects all employees with a first name starting with 'J'.
4.3 I N
SELECT *
FROM Employees
WHERE Position IN ('Analyst', 'Engineer');
5. Joining Tables
JOIN statements are used to combine rows from two or more tables based
on a related column. The different types of joins include INNER JOIN,
LEFT (OUTER) JOIN, RIGHT (OUTER) JOIN, and FULL (OUTER) JOIN.
DepartmentID DepartmentName
1 IT
2 Sales
3 HR
This query retrieves the list of employees along with their respective
department names.
6. Aggregation fiunctions
SQL provides several functions to perform calculations on data, such as
COUNT(), SUM(), AVG(), MIN(), MAX(), and GROUP_CONCAT().
SELECT COUNT(*)
FROM Orders
WHERE OrderDate BETWEEN '2023-01-01' AND '2023-12-31';
This query returns the total number of orders placed in the year 2023.
● A SELECT clause
● A FROM clause
● A WHERE clause
This query selects all employees who have made orders totaling more
than 1000.
1. INSERT
INSERT INTO Employees (EmployeeID, FirstName, LastName,
Position) VALUES (4, 'Mark', 'Anderson', 'Analyst');
8.2 UPDATE
UPDATE Employees
SET Position = 'Senior Analyst'
WHERE EmployeeID = 4;
8.3 DELETE
DELETE FROM Employees WHERE EmployeeID = 4;
This query deletes Mark Anderson's record from the Employees table.
1. Handling NULL v a l u e s
NULL value in SQL means no or zero value. Here's how you can use IS
NULL and IS NOT NULL:
SELECT *
FROM Employees
WHERE DepartmentID IS NULL;
This query selects all employees who don't belong to any department.
2. S t r i n g Functio ns
This query retrieves a full name by combining first and last names, the
position after removing leading and trailing spaces, and the length of
the first name.
SQL provides many functions to work with date and time. Some examples
include:
This query retrieves today's orders along with the query execution
time.
1. EXPLAIN
Most SQL databases support the EXPLAIN command, which shows the
execution plan of an SQL statement. This can help you understand how
your SQL query will be executed and where you can optimize it.
Rather than using SELECT *, specify the columns you need. This reduces
the amount of data that needs to be read from the disk.
10.3 Use L I M I T
If you only need a specific number of rows, use LIMIT to prevent reading
unnecessary data.
This query gets the top 10 employees with the highest salaries.