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.
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 uniquely identifies a row and cannot be null. It may consist of several columns. It prevents duplicate key values, not every kind of duplicate business record.
3. Foreign Key
A foreign key constrains non-null referencing values to match a referenced key. It enforces referential integrity; it does not automatically eliminate repeated data. Add NOT NULL separately when the relationship must be present.
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.
- Transactions and constraints: Database engines provide mechanisms for consistency and recovery. Their guarantees depend on the engine, configuration, isolation level, and how the application uses them.
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.
The question before a join
Before running a join, I would ask whether one input row should produce one output row, several, or none. A query can be valid SQL and still count the wrong thing. Stating the intended unit of each result row makes duplicated totals easier to spot than inspecting the final number alone.
Reference
PostgreSQL’s guide to constraints explains primary keys, foreign keys, and null behaviour in detail.