SQL (Structured Query Language) is a powerful language for interacting with databases. This tutorial will guide you through the fundamental building blocks of SQL: the SELECT
, FROM
, and WHERE
clauses.
SELECT
ClauseThe SELECT
clause tells the database which columns you want to retrieve from your table. It's like asking, "What information do I need?"
Example:
SELECT name, age
This statement requests the name
and age
columns from a table.
FROM
ClauseThe FROM
clause specifies the table you want to query. Think of it as declaring the source of your data.
Example:
FROM customers
This statement indicates that you're working with the customers
table.
WHERE
ClauseThe WHERE
clause filters the data based on specific conditions. It's like adding a filter to your query.
Example:
WHERE age > 25
This statement restricts the results to only customers whose age
is greater than 25.
Now, let's combine these clauses to construct a complete SQL query:
SELECT name, age
FROM customers
WHERE age > 25;
This query retrieves the name
and age
columns from the customers
table, but only for customers who are older than 25 years old.
SELECT
, FROM
, and WHERE
clauses work together to retrieve specific data from your database.WHERE
clause is optional and can be used to refine your query based on specific conditions.Let's try an example! Imagine you have a table called products
with columns like product_name
, price
, and category
. Write a SQL query to retrieve the names and prices of all products in the 'Electronics' category.