Practical Examples of Using SQL Distinct, WHERE Clause and AND, OR, and NOT operators
Written by Devanshu Agarwal /
Have you ever wanted to view a list of unique values from a particular column in your database table? That's where the "SELECT DISTINCT" statement in SQL comes in handy!
Let's say you have a table called "employees" with columns "id", "first_name", "last_name", and "department". You want to see a list of unique departments in your table. Here's how you would write your SQL query:
SELECT DISTINCT department
FROM employees;
In this example, we are selecting the "department" column from the "employees" table, and using the "DISTINCT" keyword to ensure that we only get a list of unique department names.
employee_id | first_name | last_name | department |
---|---|---|---|
1 | John | Smith | Sales |
2 | Jane | Doe | IT |
3 | Bob | Johnson | Marketing |
4 | Mike | Williams | Sales |
5 | Karen | Davis | IT |
6 | Sarah | Brown | Marketing |
7 | David | Lee | Sales |
8 | Jessica | Chen | HR |
9 | Michael | Kim | Marketing |
10 | Michelle | Lee | Sales |
And what would the output of this query look like? Here's an example:
department |
---|
HR |
IT |
Marketing |
Sales |
As you can see, the output table only includes the unique department names from the "employees" table. This is particularly useful when you have a large table with many repeated values in a certain column, and you want to quickly see a list of unique values without any duplicates.
SQL Where
Let's say you have a table of customer orders called "Orders" with columns for OrderID, CustomerID, OrderDate, and TotalAmount. You want to find all orders that were made on or after a certain date. To do this, you would use the WHERE clause.
Orders Table
OrderID | CustomerID | OrderDate | TotalAmount |
---|---|---|---|
1 | 1001 | 2022-01-01 | 50.00 |
2 | 1002 | 2022-01-02 | 75.00 |
3 | 1003 | 2022-01-03 | 100.00 |
4 | 1001 | 2022-01-04 | 25.00 |
5 | 1002 | 2022-01-05 | 150.00 |
6 | 1003 | 2022-01-06 | 200.00 |
Here's an example SQL statement using the WHERE clause to filter the Orders table:
SELECT * FROM Orders
WHERE OrderDate >= '2022-01-04';
Output
OrderID | CustomerID | OrderDate | TotalAmount |
---|---|---|---|
4 | 1001 | 2022-01-04 | 25.00 |
5 | 1002 | 2022-01-05 | 150.00 |
6 | 1003 | 2022-01-06 | 200.00 |
As you can see, the output result table includes all the rows where the OrderDate is equal to or greater than January 4th, 2022. This is just one example of how you can use the WHERE clause in SQL to filter and analyze data in a more targeted way.
SQL AND, OR and NOT Operators
SQL operators can be a bit confusing at first, but once you get the hang of them, they can be incredibly powerful tools for managing data in your databases. Let's take a look at the AND, OR, and NOT operators, and see how they work with an example table.
Employees Table
id | name | age | department |
---|---|---|---|
1 | Alice | 27 | Sales |
2 | Bob | 32 | Engineering |
3 | Charlie | 22 | Marketing |
4 | David | 45 | Finance |
5 | Emily | 38 | Engineering |
6 | Frank | 29 | Sales |
Now, let's use the AND, OR, and NOT operators to query this table.
AND
The AND operator allows us to select rows that meet multiple conditions. For example, we can use the AND operator to find employees who are in the Sales department and are over 25 years old:
SELECT * FROM employees
WHERE department = 'Sales' AND age > 25;
This will give us the following output:
id | name | age | department |
---|---|---|---|
1 | Alice | 27 | Sales |
6 | Frank | 29 | Sales |
OR
The OR operator allows us to select rows that meet one or more conditions. For example, we can use the OR operator to find employees who are either in the Engineering department or are over 40 years old:
SELECT * FROM employees
WHERE department = 'Engineering' OR age > 40;
This will give us the following output:
id | name | age | department |
---|---|---|---|
2 | Bob | 32 | Engineering |
4 | David | 45 | Finance |
5 | Emily | 38 | Engineering |
NOT
The NOT operator allows us to select rows that do not meet a certain condition. For example, we can use the NOT operator to find employees who are not in the Sales department:
SELECT * FROM employees
WHERE department <> 'Sales';
This will give us the following output:
id | name | age | department |
---|---|---|---|
2 | Bob | 32 | Engineering |
3 | Charlie | 22 | Marketing |
4 | David | 45 | Finance |
5 | Emily | 38 | Engineering |
Read More
SQL Intro: The Beginner's Guide to SQL Basics
Discover SQL syntax and learn the fundamentals of SQL for beginners. Start coding SQL databases in minutes with this comprehensive guide.
Sorting SQL data in ascending and descending order
Learn how to sort your SQL data in ascending order with the ORDER BY ASC clause and improve your query results.
SQL Practice Queries - 1
Looking to enhance ur DB skills? With our advanced sql queries, you'll learn techniques to improve your database skills.
SQL Practice Queries - 2
Looking to enhance your database management skills and take your SQL knowledge to the next level? With our advanced SQL functions and queries training, you'll learn the syntax and techniques to improv ...