English · IndiaLog in
Rackwave Technologies
Request quote Free consultation
DataUpdated 22 Nov 2026

What Is SQL? Commands, Joins and Examples Explained

A clear guide to SQL: relational data, commands, joins, aggregation, performance and learning path.

Written by Rohan Verma
  • Published22 Nov 2026
  • Read time7 min
  • Length1,444 words
Share this guide

What is SQL? SQL, short for Structured Query Language, is the standard language for storing, retrieving, changing and managing data in relational databases. With a few readable statements you can ask a database for exactly the rows you want, combine tables, summarise results and update records. Nearly every business application, reporting tool and data platform speaks SQL in some form, which is why it remains one of the most valuable skills in technology after fifty years. This guide explains what SQL is, how relational databases organise data, the core commands, joins and aggregation with worked examples, performance basics and how SQL fits into modern analytics.

Short answer: SQL lets you query relational data with statements such as SELECT, INSERT, UPDATE and DELETE. Data is organised in tables of rows and columns, and tables are linked by keys so that you can combine them with joins.

What is SQL?

SQL was developed at IBM in the 1970s and later standardised, and it has outlasted countless technologies since. It is declarative: you describe what result you want, not the steps to compute it, and the database engine decides the best way to get it. That is why the same query can run quickly on a small table and still work on a table with billions of rows.

You will hear about different flavours, such as MySQL, PostgreSQL, SQL Server, Oracle and the SQL used by cloud warehouses. They share a common core and differ in details such as date functions and extra features, so skills transfer easily between them. Our guide to the database explains the systems that SQL talks to.

How relational data is organised

Diagram of relational tables linked by keys: customers, orders and products
Figure 1. Relational databases store data in tables and connect them with keys, so each fact is stored once.
TermMeaningExample
TableA collection of rows with the same columnscustomers, orders
RowOne recordA single customer
ColumnOne attribute of every recordemail, created_at
Primary keyA column that uniquely identifies each rowcustomer_id
Foreign keyA column that points to a primary key in another tableorders.customer_id
IndexA structure that speeds up lookupsAn index on email
SchemaThe structure of tables and relationshipsThe shop database design

Splitting data across tables avoids repetition. A customer's details live in one row, and each order simply refers to the customer by key. When the customer changes their address, you change it in one place.

The main groups of SQL commands

GroupPurposeMain commands
Querying (DQL)Read dataSELECT
Data manipulation (DML)Change dataINSERT, UPDATE, DELETE
Data definition (DDL)Define structureCREATE, ALTER, DROP
Access control (DCL)Manage permissionsGRANT, REVOKE
Transaction controlGroup changes safelyBEGIN, COMMIT, ROLLBACK

Your first queries

The SELECT statement does most of the work. These examples use a simple shop database with customers, orders and order items.

-- Customers in a region, newest first
SELECT customer_id, name, email, created_at
FROM customers
WHERE region = 'APAC'
ORDER BY created_at DESC
LIMIT 20;
-- Insert, update and delete
INSERT INTO customers (name, email, region)
VALUES ('Asha Rao', 'asha@example.com', 'APAC');

UPDATE customers SET region = 'EMEA' WHERE customer_id = 42;

DELETE FROM customers WHERE customer_id = 99;

Always use a WHERE clause on UPDATE and DELETE. Without one, the statement changes or removes every row. Run the same condition as a SELECT first, check the rows returned, and use a transaction so you can roll back a mistake.

Joins: combining tables

Join typeReturnsUse when
INNER JOINRows with a match in both tablesYou only want records that relate
LEFT JOINAll rows from the left table, matches from the rightYou want everything on the left, even without matches
RIGHT JOINAll rows from the right table, matches from the leftRare; usually rewritten as a left join
FULL OUTER JOINAll rows from both tablesYou need unmatched rows from either side
CROSS JOINEvery combination of rowsGenerating combinations, used carefully
-- Revenue per customer, including customers with no orders
SELECT c.customer_id,
       c.name,
       COUNT(o.order_id)          AS orders,
       COALESCE(SUM(o.amount), 0) AS revenue
FROM customers AS c
LEFT JOIN orders AS o
       ON o.customer_id = c.customer_id
      AND o.status <> 'cancelled'
GROUP BY c.customer_id, c.name
ORDER BY revenue DESC;

The condition on cancelled orders sits in the join, not in WHERE, which keeps customers without qualifying orders in the result. Placing it in WHERE would silently turn the left join into an inner join, a very common mistake.

Aggregation and grouping

Aggregate functions such as COUNT, SUM, AVG, MIN and MAX summarise many rows into one value, and GROUP BY decides the groups. HAVING filters groups after aggregation, as WHERE filters rows before it.

SELECT region,
       COUNT(*)      AS customers,
       AVG(lifetime) AS avg_lifetime_value
FROM customer_summary
GROUP BY region
HAVING COUNT(*) >= 100
ORDER BY avg_lifetime_value DESC;

Most SQL mistakes are not syntax errors. They are questions answered correctly for the wrong set of rows.

Window functions and common table expressions

Two features lift SQL from basic to powerful. Common table expressions, written with WITH, break a complex query into named steps. Window functions calculate across related rows without collapsing them, for running totals, ranks and comparisons with the previous row.

WITH monthly AS (
  SELECT DATE_TRUNC('month', order_date) AS month,
         SUM(amount)                      AS revenue
  FROM orders
  GROUP BY 1
)
SELECT month,
       revenue,
       LAG(revenue) OVER (ORDER BY month)            AS prev_month,
       revenue - LAG(revenue) OVER (ORDER BY month) AS change
FROM monthly
ORDER BY month;

Function names for dates differ between database systems, so check the documentation of the one you use.

Making SQL fast

Indexcolumns you filter and join on
Selectonly the columns you need
Readthe execution plan
  • Create indexes on columns used in WHERE, JOIN and ORDER BY, but not on everything, because indexes slow writes.
  • Avoid SELECT * in production queries. Ask only for the columns you need.
  • Filter early and avoid applying functions to indexed columns in conditions.
  • Use EXPLAIN to see how the database runs your query and where it scans whole tables.
  • Keep statistics current and archive old data that is rarely queried.
  • Use parameterised queries from application code to avoid SQL injection, a leading web security flaw. See cyber security.

SQL and NoSQL

Relational databases suit structured data with clear relationships and strong consistency, such as orders, payments and inventory. Other systems, grouped as NoSQL, store documents, key-value pairs, graphs or wide columns and suit flexible structures and extreme scale. They are covered in what is NoSQL. Many modern platforms support both, and even NoSQL products increasingly offer SQL-like query languages.

SQL in analytics and data engineering

SQL is the common language of analytics. Analysts write it to answer questions, BI tools such as Power BI and Tableau generate it behind the scenes, and engineers use it to build the transformation layers described in data engineering. Cloud platforms keep data in a data warehouse or data lake that you query with SQL, and ETL pipelines move data into place. Python often works alongside it, as discussed in what is Python.

Salesforce uses its own query dialects for CRM data, SOQL and SOSL, which resemble SQL closely. See Salesforce Apex for how they are used in code. Our data analytics service and data warehousing service help organisations build reliable SQL-ready data foundations.

How to learn SQL

  1. Get a practice database. Install a free database or use a hosted sandbox with sample data.
  2. Learn SELECT, WHERE and ORDER BY. Filter and sort until it feels natural.
  3. Master joins. Draw the tables on paper and predict the row count before running the query.
  4. Learn GROUP BY and aggregates. Answer questions such as revenue by month.
  5. Add CTEs and window functions. These make complex analysis readable.
  6. Read execution plans. Learn why a query is slow and how an index changes it.

Key takeaways

  • SQL is the standard language for querying and managing relational data.
  • Data sits in tables linked by primary and foreign keys, and joins combine them.
  • SELECT reads data; INSERT, UPDATE and DELETE change it, and a WHERE clause protects against broad changes.
  • CTEs and window functions make advanced analysis clear.
  • Indexes, narrow column lists and execution plans keep queries fast.

Quick glossary

TermMeaning
Primary keyA unique identifier for each row
JoinCombining rows from tables by related columns
IndexA structure that speeds up lookups
CTEA named temporary result defined with WITH
TransactionA group of statements that succeed or fail together

Read next: What Is NoSQL? Types, Examples and When to Use It · What Is Data Visualization? Chart Types and Best Practice · What Is a Database? Types, Concepts and How to Choose

Frequently asked questions

What is SQL used for?

SQL is used to create and manage relational databases and to retrieve, filter, combine, summarise and update data. It underpins business applications, reporting tools, data warehouses and analytics.

What does SQL stand for?

SQL stands for Structured Query Language, the standard language for working with data in relational database management systems.

Is SQL a programming language?

SQL is a domain-specific language designed for data. It is declarative rather than general-purpose, so you describe the result you want and the database decides how to produce it.

What is the difference between SQL and MySQL?

SQL is the language. MySQL is one database system that uses the language. PostgreSQL, SQL Server and Oracle are others, each with small differences in dialect and features.

What are the main types of SQL joins?

Inner join returns matching rows, left join keeps all rows from the left table, right join keeps all from the right, full outer join keeps all from both and cross join returns every combination.

What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping. HAVING filters groups after aggregation, for example keeping only regions with more than a hundred customers.

How do I make a SQL query faster?

Index the columns you filter and join on, select only the columns you need, filter early, avoid functions on indexed columns and use EXPLAIN to inspect the execution plan.

Should I learn SQL or Python first?

For working with business data, SQL is usually the better first step because most data lives in databases. Python then adds analysis, automation and modelling on top. Many roles use both.

Was this guide helpful?Your answer helps us decide what to improve. It is not linked to you.

#what is sql#sql#sql query#sql joins#structured query language

About the author

Rohan Verma

Data Platforms Consultant

Rohan works across CDP, data warehouse, and analytics platform implementations, helping clients figure out which data infrastructure actually fits their scale rather than defaulting to whatever's currently trending.

Data Platforms & Technical Architecture

Ready to put AI and data to work?

Book a free consultation and leave with a plan, not a pitch.

Get a free consultationRequest quote