SQL queries become powerful when you can filter data to find exactly what you need and present it in a meaningful order. Two essential clauses make this possible: WHERE for filtering and ORDER BY for sorting.
The WHERE clause acts like a filter, allowing only rows that meet specific conditions to be included in your results. You can use various operators:
= for exact matches>, <, >=, <= for comparisonsAND, OR for combining conditionsLIKE for pattern matchingWhen working with multiple conditions, AND requires all conditions to be true, while OR requires at least one condition to be true.
The ORDER BY clause sorts your results based on one or more columns. By default, sorting is ascending (smallest to largest, A to Z), but you can specify DESC for descending order (largest to smallest, Z to A).
These clauses work together seamlessly. The database first applies the WHERE filter to select matching rows, then sorts those results according to your ORDER BY specification. This two-step process ensures you get exactly the data you want in the order that makes most sense for your use case.
Remember that string values in SQL must be enclosed in single quotes, and date comparisons work intuitively with ISO format dates (YYYY-MM-DD).
1-- Find all products in the 'Electronics' category priced over $100,
2-- ordered by price from highest to lowest
3SELECT name, category, price
4FROM products
5WHERE category = 'Electronics' AND price > 100
6ORDER BY price DESC;