Share This Tutorial

Views 22

Using SQL: SELECT, FROM, and WHERE Clauses

Author Zak  |  Date 2024-10-15 18:08:04  |  Category Computer Science
Back Back

Understanding SQL: SELECT, FROM, and WHERE Clauses

Introduction

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.

The SELECT Clause

The 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.

The FROM Clause

The 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.

The WHERE Clause

The 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.

Putting It All Together

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.

Key Points:

Exercise

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.