SQL Fundamentals: Operators and Ordering

Today, we'll dive deeper into SQL by exploring operators and ordering. You'll learn how to filter data using various operators and how to arrange your results in a specific order using the `ORDER BY` clause, allowing you to extract and present data more effectively.

Learning Objectives

  • Understand and apply comparison operators (=, <>, >, <, >=, <=).
  • Use logical operators (AND, OR, NOT) to combine multiple conditions.
  • Learn and implement the `ORDER BY` clause to sort result sets.
  • Combine operators and ordering to create more complex and informative queries.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Comparison Operators

Comparison operators are essential for filtering data based on specific criteria. They allow you to compare values in your database. Here's a breakdown:

  • = (Equals): Checks if two values are equal.
  • <> or != (Not Equals): Checks if two values are not equal.
  • > (Greater Than): Checks if the left-hand value is greater than the right-hand value.
  • < (Less Than): Checks if the left-hand value is less than the right-hand value.
  • >= (Greater Than or Equal To): Checks if the left-hand value is greater than or equal to the right-hand value.
  • <= (Less Than or Equal To): Checks if the left-hand value is less than or equal to the right-hand value.

Example: Assuming a table named Customers with columns like CustomerID, City, and Age:

SELECT * FROM Customers WHERE City = 'London'; -- Selects all customers from London.
SELECT * FROM Customers WHERE Age > 30; -- Selects all customers older than 30.
SELECT * FROM Customers WHERE Age <> 25; -- Selects all customers who are NOT 25.

Logical Operators: AND, OR, and NOT

Logical operators allow you to combine multiple conditions in your WHERE clause, creating more sophisticated filters.

  • AND: Requires both conditions to be true.
  • OR: Requires at least one of the conditions to be true.
  • NOT: Negates a condition (inverts its truth value).

Example:

SELECT * FROM Customers WHERE City = 'London' AND Age > 30; -- Selects customers from London who are older than 30.
SELECT * FROM Customers WHERE City = 'London' OR City = 'Paris'; -- Selects customers from either London or Paris.
SELECT * FROM Customers WHERE NOT City = 'New York'; -- Selects all customers who are NOT from New York.

Ordering Results with ORDER BY

The ORDER BY clause sorts your results. It's placed after the WHERE clause (if any). By default, it sorts in ascending order (A-Z or smallest to largest). You can specify descending order using DESC.

Syntax: ORDER BY column_name ASC|DESC

Example:

SELECT * FROM Customers ORDER BY Age; -- Sorts customers by age in ascending order (youngest to oldest).
SELECT * FROM Customers ORDER BY Age DESC; -- Sorts customers by age in descending order (oldest to youngest).
SELECT * FROM Customers ORDER BY City, Age; -- Sorts by city (A-Z), and within each city, sorts by age (youngest to oldest).

Combining Operators and Ordering

You can combine all the concepts we've learned to create powerful queries. This allows for very specific data retrieval and presentation.

Example:

SELECT * FROM Customers WHERE City = 'London' AND Age > 30 ORDER BY Age DESC; -- Selects London customers over 30 and sorts them by age (oldest to youngest).
Progress
0%