SQL Commands Explained
Introduction to SQL
What is SQL?
Databases store vast amounts of information, from user profiles on a social media site to inventory in a warehouse. But how do we communicate with them? How do we ask a database to find, add, or change information? We use a special language called SQL.
SQL, or Structured Query Language, is designed to "talk" to databases.
Think of it like this: if you want to find a book in a massive library, you ask a librarian. You use a common language, like English, to make your request. SQL is the common language for relational databases. You write commands, called queries, to tell the database exactly what you need.
A Quick History
The story of SQL begins in the early 1970s at IBM. Researchers were working on a new way to organize data called the "relational model," an idea pioneered by Edgar F. Codd. This model organized data into simple tables with rows and columns, a structure that is still the foundation of most databases today.
To manage the data in these new relational databases, they needed a language. That language was originally called SEQUEL (Structured English Query Language) and later shortened to SQL. The goal was to create something that was powerful yet relatively easy for humans to read and write.
Over the years, organizations like the American National Standards Institute (ANSI) and the International Organization for Standardization (ISO) made SQL a standard. This standardization means that while different database systems like MySQL, PostgreSQL, and SQL Server might have their own minor variations, the core SQL language works almost everywhere. It’s like different dialects of the same language.
The Basic Structure
At its heart, an SQL command is a declarative statement. You don't tell the database how to get the data; you just describe what you want. The database system figures out the most efficient way to carry out your request.
SQL commands often read like simple English sentences. They are made up of keywords, which are reserved words that have a special meaning. Let's look at the structure of a common type of query.
SELECT name
FROM users
WHERE country = 'Canada';
Even if you've never seen SQL before, you can probably guess what this command does. It asks the database to:
SELECT name: Get thenamecolumn.FROM users: Look in theuserstable.WHERE country = 'Canada': Only find rows where thecountryis Canada.
This simple, readable structure is a key reason for SQL's enduring popularity.
The Five Families of Commands
SQL commands aren't just one big pile. They are grouped into categories based on what they do. Think of these as different toolkits for specific jobs.
| Category | Full Name | Job |
|---|---|---|
| DQL | Data Query Language | Ask questions and retrieve data. |
| DML | Data Manipulation Language | Change the data (add, update, delete). |
| DDL | Data Definition Language | Define the database structure (tables). |
| DCL | Data Control Language | Manage user permissions and access. |
| TCL | Transaction Control Language | Manage groups of changes (transactions). |
Data Query Language (DQL) is for fetching data. It's the most frequently used category. Its main command is SELECT, which you use to ask the database questions.
Data Manipulation Language (DML) is for working with the data inside the tables. This includes adding new rows with INSERT, changing existing rows with UPDATE, and removing rows with DELETE.
Data Definition Language (DDL) is for building the house itself. You use DDL commands to create the database and its tables (CREATE), change their structure (ALTER), or delete them entirely (DROP).
Data Control Language (DCL) handles security. These commands determine who can see and do what in the database. You use GRANT to give permissions and REVOKE to take them away.
Transaction Control Language (TCL) manages transactions, which are sequences of operations performed as a single logical unit of work. Commands like COMMIT save all the changes in a transaction, while ROLLBACK undoes them if something goes wrong. This ensures your data stays consistent.
Understanding these five categories helps organize your thinking. When you have a task, you can first identify which family of commands you'll need to use.
What is the primary purpose of SQL (Structured Query Language)?
SQL was originally developed in the 1970s at IBM, based on the relational model pioneered by Edgar F. Codd.
That's a quick tour of what SQL is and how it's structured. You've learned about its purpose, history, and the main categories of its commands. This foundation is the first step toward managing data effectively.