SQL Foundations 04: Advanced Filtering with WHERE

We’ve seen the basic use of WHERE for single condition filters. Now, let's expand our skills to include multiple conditions and range queries.

SQL Foundations 04: Advanced Filtering with WHERE

Welcome back to Day 4 of our SQL journey at SkillSprints! Building on what we learned about the WHERE clause, today we’ll explore more advanced filtering techniques. It's time to turn up the precision in your data queries!

Deep Dive into WHERE We’ve seen the basic use of WHERE for single condition filters. Now, let's expand our skills to include multiple conditions and range queries.

Step 1: Multiple Conditions with AND, OR Using AND and OR, you can combine multiple conditions in your WHERE clause.

  1. Open phpMyAdmin: Navigate to your interface.
  2. Select Database and Table: Click on sql_sprint and then employees.
  3. SQL Query with Multiple Conditions: Try the following query to find employees in the 'IT' department with a salary over $50,000: SELECT * FROM employees WHERE department = 'IT' AND salary > 50000;
  4. Run the Query: Observe how the results now fit both conditions.
Notice both the criteria for the department and the salary are met in the returned data

Step 2: Using OR for Alternative Conditions What if you want to find employees in either the 'IT' or 'Marketing' departments? Use OR:

SELECT * FROM employees WHERE department = 'IT' OR department = 'Marketing';

Members who are either part of the IT department or the Marketing department are displayed

Step 3: Range Queries with BETWEEN For range queries, BETWEEN is your friend. It’s great for specifying a range, like dates or numbers.

SELECT * FROM employees WHERE salary BETWEEN 40000 AND 60000;

Now we can see the result using the BETWEEN criteria. This was generated using the mySQL command line

Encountered a Hitch? If things get tricky, remember our ​Slack channel ​is always open for your questions and troubleshooting needs!

Your Mission: Try creating queries with:

  • Multiple conditions using AND.
  • Alternative conditions using OR.
  • A range query using BETWEEN.

Final Thought: Mastering advanced WHERE clauses empowers you to extract precisely what you need from vast pools of data. You’re becoming more adept at asking the right questions to your database!