Learning Goals
By the end of this section you will:
know how to filter
SELECTresults using theWHEREclauseknow how to combine conditions using the
ANDandORoperatorsknow how to search for simiiar values using the
LIKEoperator and wildcardsunderstand what
NULLvalues areknow how to handle
NULLvalues
WHERE ClauseΒΆ
The SQL WHERE clause is used to filter records in a database query to include only those that meet specified conditions.
Summary
Core Function: The WHERE clause is used to extract only those records that fulfill a specified condition. It acts like a filter, allowing you to narrow down your results from a larger dataset. Versatility: The video emphasizes that the WHERE clause is not limited to SELECT statements. It is a fundamental part of other crucial SQL commands like UPDATE and DELETE, enabling you to specify which rows to modify or remove. Itβs described as a βSwiss Army knifeβ for SQL queries.
Basic Syntax: The general structure for using the clause is:
SELECT column1, column2, ... FROM table_name WHERE condition;Filtering Different Data Types:
Text Fields: When filtering based on text values (like a country name), the value must be enclosed in single quotes. For example: WHERE Country = βMexicoβ.
Numeric Fields: Numeric values do not require quotes. For example: WHERE CustomerID = 1.
Using Operators: The WHERE clause can be used with a variety of operators to create precise filtering conditions. The video lists the following common operators:
=(Equal)>(Greater than)<(Less than)>=(Greater than or equal)<=(Less than or equal)<>or!=(Not equal)BETWEEN(Within a certain range)LIKE(Search for a pattern)IN(To specify multiple possible values for a column)
The video uses practical examples from a βCustomersβ table to illustrate how to apply these concepts, making it clear and easy to follow.
Go to the W3schools Tutorial on the WHERE clause and complete their exercises.
AND and OR OperatorsΒΆ
The SQL AND operator is used to filter records by returning only rows where all specified conditions are true. While the SQL OR operator returns rows where at least one of the specified conditions is true.
Summary
The video provides a tutorial on using the AND operator in SQL to filter records based on multiple conditions. Key Concepts
Purpose: The AND operator is used in the WHERE clause to filter results from a query, ensuring that all specified conditions are met.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;Functionality: For a record to be included in the result set, every condition linked by the AND operator must be true.
Examples from the video
Selecting Customers from Spain with a name starting with βGβ:
SELECT * FROM Customers
WHERE Country = 'Spain'
AND CustomerName LIKE 'G%';Selecting Customers from Germany, in Berlin, with a Postal Code greater than 12000:
SELECT * FROM Customers
WHERE Country = 'Germany'
AND City = 'Berlin'
AND PostalCode > '12000';Combining AND and OR The video also demonstrates how to combine AND and OR operators. It emphasizes the importance of using parentheses () to group conditions and ensure the logic is evaluated correctly.
Example with parentheses (correct logic): This query finds customers from Spain whose names start with either βGβ or βRβ.
SELECT * FROM Customers
WHERE Country = 'Spain'
AND (CustomerName LIKE 'G%' OR CustomerName LIKE 'R%');Example without parentheses (incorrect logic): Without parentheses, the query would return all customers from Spain whose names start with βGβ, plus all customers from any country whose names start with βRβ.
SELECT * FROM Customers
WHERE Country = 'Spain'
AND CustomerName LIKE 'G%' OR CustomerName LIKE 'R%';AND vs. OR Operator AND: Displays a record if ALL conditions are true. OR: Displays a record if ANY of the conditions are true. The video concludes by encouraging viewers to practice using these concepts on the W3Schools website.
Go to the W3schools Tutorial on the AND, OR and NOT operators and complete their exercises.
LIKE OperatorΒΆ
Go to the W3schools Tutorial on the LIKE operator and complete their exercises.
NULL ValuesΒΆ
Summary
This video from W3Schools provides a clear and concise explanation of NULL values in SQL. Hereβs a summary of the key points covered:
What is a NULL value? A NULL value in an SQL table represents a field with no value. Itβs essentially an empty or unknown value, different from a zero or a space. It signifies that the data for that particular field is missing.
How to handle NULL values: When a field in a table is optional, you can insert or update a record without providing a value for that field. In such cases, SQL automatically assigns a NULL value to it.
Querying for NULL values: To find records with NULL values, you cannot use standard comparison operators like β=β or β<>β. Instead, you must use the IS NULL operator in your WHERE clause.
Querying for non-NULL values: Conversely, to find records where a field has a value (i.e., is not NULL), you should use the IS NOT NULL operator.
The video emphasizes that mastering the use of IS NULL and IS NOT NULL is crucial for effectively managing and querying data in SQL databases where some information might be incomplete.
Go to the - W3schools Tutorial on NULL values and complete their exercises.
WHERE Activities
Using the Movies database
Display the name of all the US directors
Display the name of the all non-US directors
Display the name of all the members who owe money
Display all the movies that have βtheβ in their title
Display all the movies that start with Z
Using the Repairs database
List the owners whose repair is ready to collect
List the owners whose iMac is still being repaired
Using the World database
Which countries have not achieved independence but still have a capital
List all details of countries that are missing information?
List countries that are either constitutional monarchies or republics