Learn SQL from Scratch
Hello! Welcome
Today we are going to explore the world of SQL, from scratch!
What is SQL?
SQL, or Structured Query Language, is a language designed to manage and manipulate databases.
It is the tool that allows you to store, query, and modify data efficiently.
Components of SQL
One of the main uses of SQL is to perform CRUD operations
- Create
- Read
- Update
- Delete
Although there are also more advanced operations (such as creating databases or managing permissions and users)
You’re doing great!
Now let’s talk about read queries
Getting Data
The most basic query in SQL is SELECT, which allows you to extract data from a database.
SELECT *
FROM employees;
Filtering Data
To get specific data, you can use the WHERE clause to filter results.
SELECT *
FROM employees
WHERE age > 30;
Sorting Data
You can sort the results with the ORDER BY clause.
SELECT *
FROM employees
ORDER BY salary DESC;
Aggregate Functions
SQL has functions to perform calculations and aggregate data, such as COUNT, SUM, AVG, MIN, and MAX.
SELECT AVG(salary)
FROM employees;
Table Joins
SQL allows you to combine data from multiple tables using JOIN (there are different types)
SELECT employees.name, departments.name
FROM employees
JOIN departments ON employees.dept_id = departments.id;
You’re almost there!
Now let’s look at some write queries for data
Inserting Data
To add data to a table, we use the INSERT INTO statement.
INSERT INTO employees (name, salary)
VALUES ('Ana', 50000);
Updating Data
You can update existing data with the UPDATE statement.
UPDATE employees
SET salary = 55000
WHERE name = 'Ana';
Deleting Data
You can also delete data with the DELETE FROM statement.
DELETE FROM employees
WHERE name = 'Ana';
Advanced Operations
Besides working with data, there are many more operations you can perform when working with databases.
For example, you can create a new table with CREATE TABLE, or create relationships between tables with FOREIGN KEY. You can even create users or change permissions!
Some of these are advanced or for administrators! You will learn them gradually!
Well Done!
Now you have the basics to start working with SQL and databases! Keep exploring and practicing.