SQL - Select Where Clause
The SELECT ... WHERE clause is used to retrieve rows from a table based on a specified condition. It allows you to filter the rows and retrieve only those that satisfy the given condition.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
Let's assume we have a "Tutorials" table with the following columns:
TutorialID (unique identifier for each tutorial)
Title (title of the tutorial)
IsFull (1 if the tutorial is complete, 0 if it's not complete)
Details (detailed content of the tutorial)
To select the full details of tutorials that are complete (IsFull = 1), we can use the following query:
SELECT TutorialID, Title, Details
FROM Tutorials
WHERE IsFull = 1;
Explanation:
The SELECT statement specifies the columns you want to retrieve from the table. In this case, we're selecting TutorialID, Title, and Details.
The FROM clause indicates the table from which we want to retrieve data. In this case, it's the "Tutorials" table.
The WHERE clause is used to specify a condition to filter the rows. Here, we're checking if the IsFull column is equal to 1, indicating a complete tutorial.
The condition IsFull = 1 is used to filter the rows and retrieve only those where the value of the IsFull column is 1 (complete tutorial).
By executing this query, you will retrieve the TutorialID, Title, and Details of tutorials that are marked as complete (IsFull = 1). Adjust the table name and column names as per your specific database schema, and modify the condition within the WHERE clause to match your desired condition.
Example 1: Retrieve tutorials with a specific TutorialID:
SELECT TutorialID, Title, Details
FROM Tutorials
WHERE TutorialID = 3;
This query selects the TutorialID, Title, and Details of the tutorial where the TutorialID is equal to 3.
Example 2: Retrieve tutorials with a specific Title:
SELECT TutorialID, Title, Details
FROM Tutorials
WHERE Title = 'Introduction to SQL';
This query selects the TutorialID, Title, and Details of the tutorial where the Title is equal to 'Introduction to SQL'.
Example 3: Retrieve tutorials with a specific condition using comparison operators:
SELECT TutorialID, Title, Details
FROM Tutorials
WHERE IsFull = 1 AND Views > 1000;
This query selects the TutorialID, Title, and Details of tutorials that are marked as complete (IsFull = 1) and have more than 1000 views (Views > 1000). It combines multiple conditions using the AND operator.
Example 4: Retrieve tutorials with a specific condition using pattern matching:
SELECT TutorialID, Title, Details
FROM Tutorials
WHERE Title LIKE '%SQL%';
This query selects the TutorialID, Title, and Details of tutorials where the Title contains the word 'SQL'. The LIKE operator with the % wildcard is used for pattern matching.
These examples demonstrate how you can use the SELECT ... WHERE clause to retrieve specific rows based on different conditions.