SQL Handbook
Welcome to the world of SQL! If you’re just starting, you’re in the right place. Whether diving into data analysis and data science or simply wanting to understand how databases work, learning SQL is an essential and fun skill.
This blog post is designed to be a quick handbook and will be the first of many of the technology handbooks that I am planning to create over the next few weeks.
What is SQL?
SQL stands for Structured Query Language. It’s a standard language used to communicate with databases. SQL allows you to store, retrieve, update, and delete data efficiently. It’s widely used in various fields, including data analysis, data science, web development, and more. You can find more here
Basic SQL Concepts
Databases and Tables
- A database is a collection of organized information.
- Within a database, data is stored in tables, which are like spreadsheets with rows and columns.
Data Types
- Data types define the kind of data you can store in a table’s column, such as integers, text, dates, etc.
Fundamental SQL Commands
SELECT: Retrieving Data The SELECT statement is used to fetch data from a database.
SELECT column1, column2
FROM table_name;Example:
SELECT first_name, last_name
FROM employees;INSERT: Adding New Data The INSERT statement is used to add new rows to a table.
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);Example:
INSERT INTO employees (first_name, last_name)
VALUES ('John','Smith');UPDATE: Modifying Existing Data The UPDATE statement is used to modify existing data in a table.
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;Example:
UPDATE employees
SET last_name = 'Cena'
WHERE employee_id = 1;DELETE: Removing Data The DELETE statement is used to remove data from a table.
DELETE FROM table_name
WHERE condition;Example:
DELETE FROM employees
WHERE employee_id = 1;Filtering and Sorting Data
WHERE Clause The WHERE clause is used to filter records.
SELECT column1, column2
FROM table_name
WHERE condition;Example:
SELECT * FROM employees
WHERE department = 'Sales';ORDER BY Clause The ORDER BY clause is used to sort the result set.
SELECT column1, column2
FROM table_name
ORDER BY column1 ASC|DESC;Example:
SELECT * FROM employees
ORDER BY last_name ASC;LIMIT Clause The LIMIT clause is used to limit the number of rows returned.
SELECT column1, column2
FROM table_name
LIMIT number;Example:
SELECT * FROM employees
LIMIT 10;Joining Tables
Introduction to Joins Joins are used to combine rows from two or more tables based on a related column.
Types of Joins
Get Deb’s stories in your inbox
Join Medium for free to get updates from this writer.
INNER JOIN: Returns records that have matching values in both tables.
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;Example:
SELECT employees.first_name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;LEFT JOIN: Returns all records from the left table, and the matched records from the right table.
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;Example:
SELECT employees.first_name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;RIGHT JOIN: Returns all records from the right table, and the matched records from the left table.
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;Example:
SELECT employees.first_name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;FULL JOIN: Returns all records when there is a match in either left or right table.
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;Example:
SELECT employees.first_name, departments.department_name
FROM employees
FULL JOIN departments
ON employees.department_id = departments.department_id;Grouping Data
GROUP BY Clause The GROUP BY clause groups rows that have the same values into summary rows.
SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1;Example:
SELECT department, COUNT(*)
FROM employees
GROUP BY department;Aggregate Functions
- COUNT: Returns the number of rows.
- SUM: Returns the total sum.
- AVG: Returns the average value.
- MAX: Returns the maximum value.
- MIN: Returns the minimum value.
Examples:
SELECT COUNT(*)
FROM employees;SELECT AVG(salary)
FROM employees;Some more advanced SQL Concepts
Subqueries A subquery is a query nested inside another query.
SELECT column1
FROM table_name
WHERE column2 IN (SELECT column2 FROM another_table WHERE condition);Example:
SELECT first_name
FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE location = 'New York');Views A view is a virtual table based on the result set of an SQL statement.
CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition;Example:
CREATE VIEW sales_view AS
SELECT first_name, last_name, sales
FROM employees
WHERE department = 'Sales';Good Reads
You can follow the below websites for a detailed learning experience —
https://www.w3schools.com/sql/
https://www.tutorialspoint.com/plsql/index.htm
Conclusion
Remember, the key to mastering SQL is practice. So, fire up your SQL editor of choice (my recommendation would be to start with Mysql) and start experimenting with queries. Happy querying!





