SQL (Structured Query Language) is the standard language used to work with relational databases. It was created by IBM in the 1970s and is still used today in many database management systems, including MySQL, PostgreSQL, SQLite, Oracle, SQL Server, and others.
As an extra resource, you can also watch my short 60-second video here: SQL video
What Is SQL Used For?
With SQL, you can work with data in a database in several important ways:
- Add new data with
INSERT - Read and search existing data with
SELECT - Update existing data with
UPDATE - Delete data that is no longer needed with
DELETE - Create and modify tables with
CREATEandALTER - Manage user permissions with
GRANTandREVOKE
These operations are often grouped under the term CRUD:
Create, Read, Update, and Delete.
Core Concepts
1. Table
Data is stored in rows and columns. Each table represents a specific type of information. For example, a Users table can store user information.
2. Primary Key
A primary key is the unique identifier for each row in a table. It helps prevent duplicates and makes it easier to reference specific records.
3. Foreign Key
A foreign key creates a relationship between two tables. It helps keep data organized and prevents unnecessary repetition.
4. Query
A query is an instruction written in SQL. You use it to retrieve, insert, update, or delete data.
Real Code Examples
The example below creates two tables, Users and Posts, and connects them with a relationship.
-- Create the Users table
CREATE TABLE Users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE
);
-- Create the Posts table and connect it to Users
CREATE TABLE Posts (
id INT PRIMARY KEY,
user_id INT,
title VARCHAR(200),
content TEXT,
FOREIGN KEY (user_id) REFERENCES Users(id)
);Adding Data
INSERT INTO Users (id, name, email)
VALUES (1, 'Ziya Mammadov', 'ziya@example.com');Searching for Data With SELECT
-- List all users
SELECT * FROM Users;
-- Find the user named Ziya Mammadov
SELECT * FROM Users WHERE name = 'Ziya Mammadov';Joining Tables
-- Show users and their posts
SELECT Users.name, Posts.title
FROM Users
JOIN Posts ON Users.id = Posts.user_id;Advantages of SQL
- Declarative: You describe what you want, and the database system decides how to get it.
- Standardized: Most relational databases use similar SQL syntax.
- Performance-friendly: Queries can be optimized with indexes and database engines.
- Reliable: SQL databases support data integrity and safety through principles such as ACID.
Conclusion
SQL is not just another programming language. It is one of the most important skills for anyone who works with data.
Whether you are a developer, analyst, or data engineer, SQL is the tool that lets you "talk" to data.
If this post was useful for you, share it and let me know what you think.