SQL, or Structured Query Language, is the universal language for managing and querying data stored in relational databases. For students and professionals in data analysis, business intelligence, or software engineering in the US and Europe, proficiency in SQL is not just a skill—it's a prerequisite.
This guide provides a concise introduction to the most fundamental SQL commands you'll use every day.
The Foundation: The SELECT Statement
The SELECT statement is how you retrieve data from a database. It's the starting point for any data query.
The basic syntax is:
SELECT column1, column2 FROM table_name;
- To select every column without listing them individually, use the wildcard asterisk (
*):-- This query retrieves all data from the 'employees' table. SELECT * FROM employees;
Filtering Your Data: The WHERE Clause
You rarely need every single row from a table. The WHERE clause is essential for filtering your data to find only the records that meet specific criteria.
To find all employees in the 'Sales' department:
SELECT first_name, last_name, email FROM employees WHERE department = 'Sales';You can use comparison operators (
=,!=,>,<) and combine conditions withANDandORfor more complex filtering.-- Find products that are in stock and cost more than $50. SELECT product_name, price FROM products WHERE price > 50.00 AND stock_quantity > 0;
Sorting Your Results: The ORDER BY Clause
The ORDER BY clause sorts your result set based on one or more columns. By default, it sorts in ascending order (ASC).
To list products from cheapest to most expensive:
SELECT product_name, price FROM products ORDER BY price ASC;To list employees by hire date, starting with the most recent, use
DESCfor descending order:SELECT first_name, last_name, hire_date FROM employees ORDER BY hire_date DESC;
Aggregating Data: GROUP BY and Aggregate Functions
This is where data analysis truly begins. Aggregate functions perform a calculation on a set of values and return a single value. Common functions include COUNT(), SUM(), AVG(), MIN(), and MAX().
The GROUP BY clause groups rows that have the same values in specified columns into summary rows.
- To count the number of employees in each department:
SELECT department, COUNT(employee_id) AS number_of_employees FROM employees GROUP BY department;
Combining Data from Multiple Tables: JOINs
Real-world databases are normalized, meaning data is split into multiple related tables to reduce redundancy. The JOIN clause is used to combine these tables back together. The most common type is INNER JOIN, which returns records that have matching values in both tables.
- To get a list of orders with the name of the customer who placed it:
This query links theSELECT orders.order_id, customers.customer_name, orders.order_date, orders.order_total FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id;orderstable and thecustomerstable on their sharedcustomer_idcolumn.
These commands are the bedrock of SQL. Mastering them will allow you to extract powerful insights from any relational database. To get more hands-on practice, you can use our AI Tutor to generate practice problems and explain more advanced concepts.