SQL Basics: SELECT, FROM, and WHERE Statements
A beginner's guide to understanding the fundamental building blocks of SQL queries.
What is SQL?
SQL (Structured Query Language) is the standard language for interacting with relational databases. Whether you're retrieving data, updating records, or analyzing information, SQL is the tool that makes it possible. Think of SQL as a specialized language that allows you to "talk" to databases and tell them exactly what information you need.
At its core, SQL is designed to be readable and somewhat similar to English, making it more approachable than many programming languages. The basic structure of an SQL query includes several key components, with the most fundamental being the SELECT, FROM, and WHERE statements.
Basic SQL Query Structure
The most common SQL operation is querying data, which follows this basic pattern:
-- Basic SQL query structure
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Let's break down each component:
- •
SELECT
: Specifies which columns you want to retrieve - •
FROM
: Indicates which table(s) to query data from - •
WHERE
: Filters the results based on specified conditions
The SELECT Statement
The SELECT statement is the starting point of most SQL queries. It specifies which columns you want to retrieve from the database.
-- SELECT examples
-- Select specific columns
SELECT first_name, last_name, email FROM customers;
-- Select all columns
SELECT * FROM customers;
-- Select with column alias
SELECT first_name AS fname, last_name AS lname
FROM customers;
Key Points About SELECT
- •Use commas to separate multiple columns
- •Use
*
as a wildcard to select all columns (use sparingly in real applications) - •Use
AS
keyword to create column aliases for more readable results - •Column order in the SELECT statement determines the order in the results
The FROM Statement
The FROM statement specifies which table or tables to query data from. This is where you tell the database where to find the columns you've requested in the SELECT statement.
-- FROM examples
-- Basic table selection
SELECT * FROM customers;
-- Using table alias
SELECT c.first_name, c.last_name
FROM customers AS c;
-- Multiple tables
SELECT customers.name, orders.order_date
FROM customers, orders
WHERE customers.id = orders.customer_id;
Working with Tables
- •Every query must have a FROM clause (except for simple calculations)
- •Table aliases (using
AS
) make queries more readable, especially with multiple tables - •When querying multiple tables, you'll typically use joins (covered in more advanced articles)
The WHERE Statement
The WHERE statement filters your query results based on specific conditions. This is how you narrow down the data to just what you need.
-- WHERE examples
-- Simple comparison
SELECT * FROM products WHERE price > 50;
-- Text matching
SELECT * FROM customers WHERE city = 'New York';
-- Multiple conditions
SELECT * FROM orders
WHERE order_date >= '2023-01-01'
AND total_amount < 100;
Filtering with WHERE
- •The WHERE clause is optional, but highly recommended to limit results
- •Conditions evaluate to TRUE, FALSE, or UNKNOWN (for NULL values)
- •Only rows where the condition evaluates to TRUE are included in the result
- •String values are typically case-sensitive and must be enclosed in quotes
Logical Operators
Combine multiple conditions using logical operators:
-- Logical operators
-- Using AND
SELECT * FROM employees
WHERE department = 'Sales'
AND hire_date > '2022-01-01';
-- Using OR
SELECT * FROM products
WHERE category = 'Electronics'
OR price > 100;
-- Using NOT
SELECT * FROM customers
WHERE NOT country = 'USA';
Comparison Operators and Special Conditions
Use various operators to create precise filtering conditions:
-- Comparison operators
SELECT * FROM products WHERE price >= 50; -- Greater than or equal
SELECT * FROM products WHERE price < 100; -- Less than
SELECT * FROM products WHERE price <> 0; -- Not equal to
-- BETWEEN operator
SELECT * FROM products
WHERE price BETWEEN 10 AND 50;
-- IN operator
SELECT * FROM customers
WHERE country IN ('USA', 'Canada', 'Mexico');
-- LIKE operator (pattern matching)
SELECT * FROM customers
WHERE email LIKE '%gmail.com'; -- Ends with gmail.com
Understanding Data Types
When writing SQL queries, it's important to understand different data types:
Common Data Types
- •TEXT: String values (enclosed in quotes)
- •INTEGER: Whole numbers
- •DECIMAL/NUMERIC: Numbers with decimals
- •DATE/DATETIME: Date and time values
- •BOOLEAN: True/false values
Working with Values
- •Text values need single or double quotes:
'apple'
or"apple"
- •Numbers don't need quotes:
42
,3.14
- •Dates typically use quotes and a specific format:
'2023-05-25'
- •Boolean values are typically
TRUE
orFALSE
(some databases use 1/0)
SQL Best Practices for Beginners
Query Writing Tips
- •Use clear, consistent formatting
- •Be specific with column selection (avoid
SELECT *
in production) - •Use meaningful aliases for clarity
- •Start with simple queries and build up complexity
Common Mistakes to Avoid
- •Forgetting quotes around text values
- •Missing commas between column names
- •Using incorrect comparison operators
- •Complex queries without proper testing
Practice Examples
Try these examples to practice basic SQL statements:
Finding All Products in a Category
SELECT name, price FROM products WHERE category = 'Electronics';
Customers from Specific Regions
SELECT first_name, last_name, city FROM customers WHERE country IN ('USA', 'Canada');
Recent Orders
SELECT order_id, customer_id, order_date FROM orders WHERE order_date >= '2023-01-01';
Next Steps
Now that you understand the basics of SQL queries, continue your learning journey: