What Is SQL? Structured Query Language Explained
Learn what SQL is, how relational databases and RDBMSs work, common SQL statements, database products, and practical SQL examples.
What Is SQL?
SQL stands for Structured Query Language. It is the language used to communicate with, access, query, and manage data in relational database systems.
A query is a request sent to a database. A query can request information, such as a customer's email address, or perform an operation, such as adding a new order. SQL can also define database structures and control access to database objects.
SQL is a database language, not a general-purpose programming language like JavaScript, Python, or Java. It is designed specifically for working with data and the software that stores and manages that data.
Why SQL Matters
Most applications need to store and retrieve data. A website may save user accounts, a mobile app may store messages, and an online store may manage products, customers, and orders. SQL provides a standard way to work with this information.
- Developers use SQL to connect application features to stored data.
- Designers and analysts use SQL to inspect data and produce reports.
- Database administrators use SQL to define structures, manage access, and maintain databases.
- Technical learners use SQL to understand how relational data is organized and connected.
SQL is useful because it can describe the result you want without requiring you to specify every low-level step for locating that data. For example, a SELECT statement can request customers from a particular city, and the database system determines how to find the matching rows.
Databases, Tables, and Relational Data
A database is an organized collection of data managed by database software. In a relational database, data is commonly organized into tables. A table stores related data in rows and columns.
- A row, also called a record, represents one individual entry, such as one customer.
- A column, also called a field or attribute, represents a named type of information stored for each record, such as an email address.
- A relationship is a connection between tables, commonly represented by matching key values.
For example, a Customers table might contain customer_id, customer_name, and email columns. An Orders table might contain order_id, customer_id, and order_date. The shared customer_id value can connect each order to its customer.
What Is an RDBMS?
An RDBMS is a Relational Database Management System. It is software used to create, store, organize, secure, and query relational databases.
An RDBMS manages tables, relationships, indexes, users, permissions, and the execution of SQL statements. It may also provide transactions, backups, views, and stored procedures.
| Concept | What it is | Examples |
|---|---|---|
| SQL | A language for working with relational databases. | SELECT, INSERT, CREATE TABLE |
| RDBMS | Software that manages relational databases and executes SQL. | MySQL, Microsoft SQL Server, PostgreSQL |
What Can SQL Do?
SQL supports both everyday data operations and database administration. The exact features and syntax depend on the database product.
| Task | Typical SQL statement | Purpose |
|---|---|---|
| Retrieve data | SELECT | Read matching data from one or more tables. |
| Add data | INSERT | Add a new record. |
| Modify data | UPDATE | Change existing records. |
| Remove data | DELETE | Remove records from a table. |
| Create structures | CREATE | Create databases, tables, views, or other objects. |
| Remove structures | DROP | Remove a database object. |
| Create views | CREATE VIEW | Save a query as a named database object. |
| Control access | GRANT and REVOKE | Give or remove permissions. |
Common Categories of SQL Statements
Data Query Language: DQL
Data Query Language (DQL) refers primarily to statements that retrieve data. The central DQL statement is SELECT.
SELECT customer_name, email
FROM Customers;
This query requests the customer_name and email columns from the Customers table. Filtering with a WHERE clause, sorting with ORDER BY, and using joins can make the result more specific.
Data Manipulation Language: DML
Data Manipulation Language (DML) changes the records stored in tables. Its main statements are INSERT, UPDATE, and DELETE.
INSERT INTO Customers (customer_name, email)
VALUES ('Ava Patel', 'ava@example.com');
This INSERT statement adds a new customer.
UPDATE Customers
SET email = 'ava.patel@example.com'
WHERE customer_name = 'Ava Patel';
This UPDATE statement changes an existing email address.
DELETE FROM Customers
WHERE customer_name = 'Ava Patel';
This DELETE statement removes the matching customer record.
Data Definition Language: DDL
Data Definition Language (DDL) defines and changes database structures. Common DDL statements include CREATE, ALTER, and DROP.
CREATE TABLE Customers (
customer_id INT,
customer_name VARCHAR(100),
email VARCHAR(255)
);
This creates a table and defines its columns. See the guide to the SQL CREATE TABLE statement for more detail. A CREATE DATABASE statement can create a database where the platform and the user's permissions allow it.
DROP TABLE Customers;
DROP removes the table itself, including its structure. It is different from DELETE, which removes rows while leaving the table in place. Use ALTER TABLE to change an existing table's structure.
Data Control Language: DCL
Data Control Language (DCL) manages access to database objects. GRANT gives a user or role a permission, while REVOKE removes one.
GRANT SELECT ON Customers TO reporting_user;
This example grants read access, but privilege names, role names, and exact syntax vary among database products.
Transaction Control
Many RDBMSs also support transaction control statements such as COMMIT and ROLLBACK. A transaction groups related changes so they can be permanently saved or undone together.
Views and Stored Procedures
A view is a named database object based on a query. It can provide a reusable, simplified, or controlled representation of data without requiring users to repeat the underlying query.
CREATE VIEW CustomerContacts AS
SELECT customer_name, email
FROM Customers;
Applications or reporting users can query CustomerContacts as a convenient data source. A view can also hide columns that a user should not normally access.
A stored procedure is a saved, executable routine in a database system. Procedures can contain SQL statements and, depending on the product, procedural logic and parameters. Stored procedures are product-dependent, so their creation syntax is not identical everywhere.
CREATE PROCEDURE GetCustomerContacts
AS
SELECT customer_name, email FROM Customers;
The exact example above is not portable to every RDBMS. Consult the documentation for the target product before creating procedures.
SQL Standards and Dialects
SQL has an ANSI standard. ANSI, the American National Standards Institute, participates in the standardization of SQL and other technologies.
Database products implement the standard with their own SQL dialect, meaning a product-specific variation of SQL. Dialects may differ in:
- Function names and built-in functions
- Data types
- Date and string expressions
- Pagination and result-limiting syntax
- Procedures, triggers, and other programming features
- Administrative commands and permission systems
The fundamental concepts transfer well: tables, rows, columns, keys, filtering, sorting, inserting, updating, and deleting. However, test statements against the documentation for the RDBMS you use.
Examples of Relational Database Systems
| Product | Relationship to SQL | Notes |
|---|---|---|
| MySQL | Uses a MySQL SQL dialect. | Common in web application development. |
| Microsoft SQL Server | Uses Microsoft's SQL dialect and tooling. | Often used in enterprise and Microsoft-based environments. |
| PostgreSQL | Uses PostgreSQL's SQL dialect. | Known for extensive relational and advanced data features. |
| Oracle Database | Uses Oracle SQL and procedural extensions. | Widely used in large-scale organizational systems. |
| SQLite | Implements SQL inside a lightweight embedded database engine. | Often used in mobile apps, desktop software, and local storage. |
How SQL Is Used in Practice
Consider an online store. An application can use SQL to read a customer's details, retrieve products, and find orders associated with that customer. When the customer changes an address, the application can issue an UPDATE. When a new order is placed, it can use INSERT statements to add order records.
An administrator can use SQL to create the tables and relationships, add constraints, create views for reporting, and grant only the permissions required by each application or user.
A typical flow looks like this:
- An application receives a request, such as “show this customer's orders.”
- The application sends a parameterized SQL query to the RDBMS.
- The RDBMS checks permissions, finds the relevant rows, and executes the query.
- The RDBMS returns result rows to the application.
- The application formats the result for a web page, mobile screen, report, or API response.
Safety Tips for Data Changes
Be especially careful with UPDATE and DELETE. A missing or overly broad WHERE condition can change or remove many records.
SELECT *
FROM Customers
WHERE customer_name = 'Ava Patel';
Run a checking SELECT first to verify which rows match. Then use a sufficiently specific condition in the data-changing statement. In production systems, transactions, backups, testing, and parameterized queries provide additional protection.
Common SQL Confusions and Errors
Confusing SQL with a database product
SQL is the language. MySQL, Microsoft SQL Server, PostgreSQL, Oracle Database, and SQLite are products that use or implement SQL.
Assuming all SQL works identically everywhere
Basic statements are often similar, but functions, data types, procedures, administrative commands, and permissions can differ. Use the target RDBMS's documentation when a statement produces a syntax or feature error.
Updating or deleting too many records
Check the intended rows with SELECT before running UPDATE or DELETE. Review the WHERE clause carefully.
Permission denied
A permission error usually means the database user lacks the required authorization. A database administrator may need to grant access to the relevant table, view, procedure, or other object.
Using DROP when only data removal is intended
DELETE removes rows. DROP removes the table, database, or other database object itself. Because DROP is destructive, confirm the target before executing it.
Key Takeaways
- SQL means Structured Query Language.
- SQL is used to communicate with and manage relational databases.
- An RDBMS is the software that manages relational databases and executes SQL.
- Relational data is organized into tables made of rows and columns.
SELECTretrieves data;INSERT,UPDATE, andDELETEchange records.CREATE,ALTER, andDROPmanage database structures.GRANTandREVOKEmanage permissions.- SQL concepts are portable, but exact syntax varies between SQL dialects.
After learning the basic idea, continue with SQL syntax, the SELECT statement, WHERE filtering, and the CREATE TABLE statement.