VMware ESXi and vSphere Cluster Management
What Is SQL? Structured Query Language Explained
Learn what SQL means, how it works with relational databases and RDBMS software, what SQL commands do, and why SQL matters to developers, administrators, and analysts.
SQL stands for Structured Query Language. It is a widely used language for defining, querying, and managing data stored in databases, especially relational databases.
With SQL, you can retrieve records, add new data, change existing data, remove data, create tables, define relationships, create views, manage permissions, and perform other database operations. SQL is an industry-standard language, although each database product may implement its own SQL dialect and extensions.
What Is a Database?
A database is an organized collection of data managed by database software. Applications use databases to store information such as customer accounts, products, orders, messages, payments, and user preferences.
Instead of keeping every value in separate files, an application can use a database to organize data, search it efficiently, enforce rules, control access, and allow multiple users or services to work with it.
What Is a Relational Database?
A relational database organizes data into tables. Each table usually represents a type of thing, such as customers, products, or orders.
- A table is a database object containing related data arranged in rows and columns.
- A row represents one record, such as one customer.
- A column represents an attribute or field, such as an email address or registration date.
- A record is the complete collection of field values for one item and is commonly represented by a row.
For example, a customers table might contain customer_id, customer_name, email, and city columns. One row could contain the details for a single customer.
Keys and relationships
Tables can be connected through relationships. A primary key is a column, or combination of columns, that uniquely identifies each row. In a customer table, customer_id could be the primary key.
A foreign key is a column that references a key in another table. An orders table can store customer_id as a foreign key pointing to the customer who placed each order.
customers orders
--------- ------
customer_id <- primary key order_id
customer_name customer_id <- foreign key
email order_dateRelated tables are usually preferable to storing all information in one large table. Separating customers and orders reduces repeated customer data, makes updates more consistent, and allows one customer to have many orders. This approach also supports data integrity rules, such as requiring every order to refer to an existing customer.
What Does RDBMS Mean?
RDBMS means Relational Database Management System. An RDBMS is software used to create, store, organize, secure, and query relational databases.
Examples of RDBMS products include MySQL, Microsoft SQL Server, PostgreSQL, Oracle Database, and SQLite. These products provide database storage and processing capabilities, while SQL provides the language used to request and manage the data.
| Concept | Meaning | Examples |
|---|---|---|
| SQL as a language | A language for defining, querying, and managing relational data. | SELECT, INSERT, CREATE TABLE |
| RDBMS as software | Software that manages relational databases and executes SQL. | MySQL, Microsoft SQL Server |
| Database | An organized collection of data managed by database software. | An application database |
| Table | A related set of rows and columns. | customers, orders |
SQL and an RDBMS are therefore not the same thing. SQL is the language; the RDBMS is the database management software that implements and executes that language.
What Is SQL Used For?
SQL can work with both the data inside a database and the database structures that contain it. The following are common uses.
| Task | Purpose | Typical SQL command |
|---|---|---|
| Retrieve data | Answer questions and return matching records. | SELECT |
| Add records | Insert new rows into a table. | INSERT |
| Change records | Modify values in existing rows. | UPDATE |
| Remove records | Delete rows from a table. | DELETE |
| Create structures | Create databases, tables, views, and other objects. | CREATE |
| Change structures | Change an existing database object. | ALTER |
| Remove structures | Remove a database object. | DROP |
| Create views | Save a query as a reusable virtual table. | CREATE VIEW |
| Create stored procedures | Save executable database routines. | CREATE PROCEDURE, where supported |
| Manage access | Assign or remove permissions on database objects. | GRANT and REVOKE |
Querying data with SELECT
A query is a SQL request for data or an operation on database data. The SELECT statement retrieves data from one or more tables.
SELECT * FROM customers;The asterisk means “all columns.” You can select only the fields needed by an application or report and filter the results with a condition.
SELECT customer_name, email
FROM customers
WHERE city = 'London';This query returns the name and email of customers whose city is London.
Adding, changing, and deleting data
INSERT adds a new row:
INSERT INTO customers (customer_name, email)
VALUES ('Ava Patel', 'ava@example.com');UPDATE changes values in existing rows. A precise WHERE condition is essential:
UPDATE customers
SET email = 'ava.patel@example.com'
WHERE customer_id = 101;DELETE removes rows. It should also use a carefully checked condition:
DELETE FROM customers
WHERE customer_id = 101;Creating and removing database objects
SQL can define database structures. For example, this statement creates a table with a primary key and two text columns:
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(100),
email VARCHAR(255)
);CREATE DATABASE can create a database in products that support the command and where the account is authorized to do so. ALTER changes an existing object, while DROP removes one.
DROP TABLE customers;DROP TABLE is destructive: it removes the table structure and typically its data. Exact behavior and recovery options depend on the RDBMS, so destructive commands should be tested and authorized before use.
Views
A view is a named query that presents data as a virtual table. A view generally stores the query definition rather than a separate copy of the result data.
CREATE VIEW customer_contacts AS
SELECT customer_id, customer_name, email
FROM customers;Applications or reporting users can query customer_contacts instead of repeating the full query. Views can also expose only selected columns or rows.
Stored procedures
A stored procedure is a saved, executable database routine containing SQL statements. Procedures can encapsulate repeated operations, validation, or business logic inside the database.
CREATE PROCEDURE ...The exact procedure syntax, parameter rules, and features vary substantially between RDBMS products. Consult the documentation for the target product before using this command.
Permissions
A permission is an authorization that determines whether a user or role can access or modify a database object. SQL can assign permissions with GRANT and remove them with REVOKE.
GRANT SELECT ON customers TO reporting_user;This example gives reporting_user permission to read from customers. Supported privileges and syntax differ by product. Use the least access needed for each account.
Major Categories of SQL Operations
SQL statements are often grouped into categories. The boundaries and terminology can vary slightly, but these categories are useful for learning.
| Category | Role | Representative commands |
|---|---|---|
| Data Query Language concepts | Retrieve data from tables and other objects. | SELECT |
| Data Manipulation Language | Add, modify, and remove table data. | INSERT, UPDATE, DELETE |
| Data Definition Language | Create or change database structures. | CREATE, ALTER, DROP |
| Data Control Language | Manage access to database objects. | GRANT, REVOKE |
| Transaction control | Confirm or undo a group of related operations where supported. | COMMIT, ROLLBACK |
A transaction is a group of database operations treated as one unit. COMMIT confirms the changes. ROLLBACK undoes changes that have not been committed, when the operation and storage engine support transactional behavior.
SQL Standards and Database Dialects
SQL has an ANSI standard. ANSI stands for American National Standards Institute, an organization associated with the SQL standard. The standard provides a common foundation for SQL syntax and behavior.
Database products support this common foundation but may add product-specific syntax, functions, data types, transaction behavior, and administrative features. These product variations are called SQL dialects.
As a result, a simple SELECT statement may work with little or no change across several products, while date functions, pagination syntax, stored procedures, identity columns, and administrative commands may differ. Do not assume that every SQL statement works identically in every RDBMS.
A Simple SQL Workflow
- Connect to an RDBMS. Use a database client, application driver, command-line tool, or management interface with an authorized account.
- Select the target database or schema. The exact command or interface depends on the RDBMS.
- Inspect the tables and relationships. Identify relevant columns, primary keys, foreign keys, and constraints.
- Query the data. Start with
SELECTto understand the current records. - Modify data only when authorized. Use
INSERT,UPDATE, orDELETEwith precise conditions. - Validate the result. Run a follow-up query and check affected-row counts, constraints, and application behavior.
- Commit or roll back when appropriate. Keep changes in a transaction when the RDBMS and operation support that workflow.
SQL statements commonly operate on sets of rows. For example, one UPDATE can change every row matching a condition instead of requiring application code to process each record individually. This set-based behavior is one of SQL's most important ideas.
Why SQL Knowledge Matters
SQL is useful wherever software stores structured information.
- Developers use SQL to read application data, create records, update user information, and support features such as search and reporting.
- Database administrators use SQL to define structures, manage access, inspect data, maintain integrity, and perform operational tasks.
- Web and mobile developers use SQL through application database drivers or frameworks to connect user actions with persistent data.
- Designers and analysts use SQL to inspect data, answer business questions, and build reports or reusable views.
Even when an application uses an object-relational mapper or another abstraction layer, understanding SQL helps you recognize inefficient queries, interpret errors, verify results, and design reliable data models.
Common SQL Problems and How to Troubleshoot Them
An UPDATE or DELETE affects too many rows
The statement may have no WHERE clause, or its condition may be broader than intended.
SELECT *
FROM customers
WHERE city = 'London';Run a selection using the intended condition first. Use a transaction, backup, or tested recovery procedure where appropriate. A missing or incorrect condition can change or remove an entire table's data.
A statement works in one database product but fails in another
The statement may use a product-specific function, data type, pagination feature, identifier rule, or stored procedure syntax. Check the target RDBMS documentation and separate standard SQL from vendor extensions.
A user cannot query or modify a table
The account may lack the required permission. Verify the user's roles and granted privileges, then provide only the access needed for the task. Permission errors can also result from connecting to the wrong database or schema.
A row cannot be added because related data does not exist
A foreign key may require a referenced parent row first. This is part of referential integrity: relationships must point to valid records. Insert or verify the parent record before inserting the child record, subject to the application's rules.
A query returns duplicate-looking rows after combining tables
The join condition may be incomplete or incorrect, or the relationship may legitimately allow multiple matching rows. Inspect the primary and foreign keys, confirm the intended relationship, and verify whether the result should contain one row or many rows per parent.
SQL, Tables, and Application Data: A Small Example
Imagine an online shop with two tables:
customersstores one row per customer and usescustomer_idas its primary key.ordersstores orders and usescustomer_idas a foreign key.
An application can use SQL to find all customers in London, insert a new order, update an email address, or generate a report connecting customers to their orders. The database can enforce rules that prevent an order from referring to a customer that does not exist.
Key Points to Remember
- SQL means Structured Query Language.
- SQL is a language for defining, querying, and managing data, primarily in relational databases.
- A relational database organizes data into related tables made of rows and columns.
- Primary keys identify rows; foreign keys connect related tables.
- An RDBMS is database management software, such as MySQL or Microsoft SQL Server; SQL is the language it implements.
SELECTretrieves data, whileINSERT,UPDATE, andDELETEmanipulate data.CREATE,ALTER, andDROPdefine or change database structures.GRANTandREVOKEmanage permissions.COMMITandROLLBACKcontrol transactions where supported.- SQL has an ANSI standard, but products also provide different SQL dialects.
- Always check the rows selected before running a significant
UPDATEorDELETE.