Notes
Chapter 15: Filtering Data with WHERE Clause

1. Conditional Filtering

The WHERE clause in Microsoft SQL Server is a powerful tool for filtering data based on specified conditions. It allows you to retrieve only the rows that meet specific criteria.

Example :

Basic WHERE clause
SELECT FirstName, LastName
FROM Employees WHERE Salary > 50000;

In this example, the WHERE clause filters employees' first names and last names based on a condition where the salary is greater than $50,000.

2. Combining Conditions

You can combine multiple conditions in a WHERE clause using logical operators like AND and OR. This allows for more complex filtering.

Combining Conditions:

Example :

combining conditions with AND
SELECT ProductName,Price
FROM EmployeeID, FirstName, LastName
FROM Products WHERE Category = 'Electronics' AND Price < 1000;

This query retrieves product names and prices for electronic products priced under $1000.

Example :

combining conditions with OR
SELECT ProductName,Price
FROM EmployeeID, FirstName, LastName
FROM Products WHERE Category = 'Electronics' OR Category = 'Appliances';

This query retrieves product names and prices for products in either the "Electronics" or "Appliances" category

3. Real-world Filtering Scenarios

E-commerce System:

E-commerce System:
  • Product Recommendations: Use WHERE to filter and recommend products based on user preferences, such as category, price range, and brand.
  • Order Tracking: Allow customers to track their orders by filtering order details based on order IDs and customer information.
Healthcare Records System:
  • Patient Records: Retrieve patient records by filtering data based on patient ID, date of birth, or medical condition.
  • Appointment Scheduling: Filter available appointment slots based on the doctor's availability and patient preferences.

4. Key Takeaways

  • The WHERE clause is used to filter data in SQL Server based on specified conditions.
  • You can combine multiple conditions using logical operators like AND and OR for more complex filtering.
  • Real-world scenarios often involve using the WHERE clause to retrieve relevant data for various purposes, such as recommendations or tracking.

5. Exercise Questions

  1. Write a query to retrieve customer names and order IDs for customers who have placed orders in the last month.
  2. Create a query that displays employee names and salaries for employees with salaries between $40,000 and $60,000.
  3. Find all products that belong to the "Electronics" category and have a price greater than $500.
  4. Generate a report that lists all patients with a specific medical condition (e.g., "Diabetes") using the WHERE clause.
  5. Explain the difference between using AND and OR in a WHERE clause with real-world examples.
  6. Discuss the importance of optimizing query performance when filtering large datasets with the WHERE clause.
Joins

In Microsoft SQL Server, a JOIN operation combines rows from two or more tables based on a related column between them. It is a fundamental concept in database management that allows you to retrieve and analyze data from multiple tables.

Read More
GROUP BY and Aggregate Functions

The GROUP BY clause in Microsoft SQL Server is used to group rows with similar values in one or more columns into summary rows. It is essential for aggregating and summarizing data.

Read More