Every time a customer places an order on an ecommerce store, that data lands in a database. SQL is how you get it back out. Whether you need a list of recent orders, a count of customers by city, or total revenue for the month, SQL is the tool you reach for.
What SQL Actually Does
A database stores data in tables. Think of each table as a spreadsheet with rows and columns. The orders table has one row per order. The customers table has one row per customer.
SQL lets you ask questions against those tables. You write a query, the database runs it, and you get rows back. That is the entire loop.
Here is the simplest possible query:
SELECT *
FROM customers;
This returns every row and every column from the customers table. The means "all columns." You will almost never use in production, but it is useful for exploring a table you have never seen before.
To get just specific columns, name them:
SELECT first_name, last_name, email
FROM customers;
SELECT (what columns you want) and FROM (which table to look in). Everything else builds on top of those two.Filtering Rows with WHERE
Returning every row is rarely useful. The WHERE clause limits results to rows that match a condition.
SELECT first_name, last_name, email
FROM customers
WHERE country = 'US';
You can stack conditions with AND and OR:
SELECT order_id, status, total_amount
FROM orders
WHERE status = 'delivered'
AND total_amount > 100;
This returns only delivered orders where the total_amount is over 100.
What SQL Cannot Do
SQL reads and writes structured data in tables. It does not build user interfaces, send emails, or run application logic on its own. It does one thing well: answer questions about data.
Common Mistakes
-- wrong
SELECT firstname, lastname
FROM customers;
-- correct
SELECT first_name, last_name
FROM customers;
Column names must match exactly what is in the database. firstname does not exist; first_name does. A typo here gives you an error, not bad data.
-- wrong
SELECT first_name, last_name, email
FROM customers
WHERE country = US;
-- correct
SELECT first_name, last_name, email
FROM customers
WHERE country = 'US';
Text values in WHERE conditions must be wrapped in single quotes. Without quotes the database treats US as a column name and throws an error.
-- wrong
SELECT *
FROM Orders;
-- correct
SELECT *
FROM orders;
Table names are case-sensitive in SQLite. Orders and orders are not the same thing.
Quick Recap
- SQL is a language for querying data stored in relational database tables.
- Every query starts with
SELECT(columns) andFROM(table). WHEREfilters rows so you only get back what you need.- Column and table names must match the database exactly, including case.
first_name, last_name, and loyalty_tier columns for every row in the customers table.SELECT with the three column names and FROM customers. No filter needed.SELECT first_name, last_name, loyalty_tier
FROM customers;
order_id, status, and total_amount from the orders table, but only for orders where the status is 'shipped' and the total_amount is greater than 50.WHERE with two conditions joined by AND. Check the column name total_amount exactly.SELECT order_id, status, total_amount
FROM orders
WHERE status = 'shipped'
AND total_amount > 50;
first_name, last_name, and the total number of orders they have placed. Name the count column order_count. Only include customers who have placed more than 2 orders. Join the customers table with the orders table using customer_id.customers and orders on customer_id. Use COUNT(o.order_id) and GROUP BY the customer columns. Filter the grouped results with HAVING.SELECT c.first_name, c.last_name, COUNT(o.order_id) AS order_count
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name
HAVING COUNT(o.order_id) > 2;
SQL stands for Structured Query Language. It is the standard language for reading and writing data in relational databases.
SQL is used to retrieve, insert, update, and delete data stored in a database. Analysts use it daily to answer business questions from raw data.
No. The basics read almost like plain English. Most people can write useful queries within a few hours of practice.
SQL is the language. SQLite is one database engine that understands SQL. Other engines include PostgreSQL and MySQL, but they all use SQL with minor differences.