SELECT COMMAND EXERCISE FOR NORTHWIND DATABASE

SELECT paired with the noble WHERE clause. Here are 50 MySQL practice exercises that use only SELECT with WHERE operators. No joins, no groupings—just pure filtering logic. This will hone your instinct like a whetstone sharpens a blade.


🔹 Basic Comparisons

  1. Select customers from the USA.

    SELECT * FROM Customers WHERE Country = 'USA';
    
  2. List products with price > 100.

    SELECT * FROM Products WHERE UnitPrice > 100;
    
  3. Show employees hired before 1995.

    SELECT * FROM Employees WHERE HireDate < '1995-01-01';
    
  4. Get orders shipped after 1997-01-01.

    SELECT * FROM Orders WHERE ShippedDate > '1997-01-01';
    
  5. Products with stock less than 10.

    SELECT * FROM Products WHERE UnitsInStock < 10;
    

🔹 Logical Operators (AND, OR, NOT)

  1. Customers from France and Paris.

    SELECT * FROM Customers WHERE Country = 'France' AND City = 'Paris';
    
  2. Suppliers from Germany or Japan.

    SELECT * FROM Suppliers WHERE Country = 'Germany' OR Country = 'Japan';
    
  3. Products not discontinued.

    SELECT * FROM Products WHERE Discontinued = 0;
    
  4. Orders not shipped to Brazil.

    SELECT * FROM Orders WHERE ShipCountry != 'Brazil';
    
  5. Employees not from USA or UK.

SELECT * FROM Employees WHERE NOT (Country = 'USA' OR Country = 'UK');

🔹 LIKE and Pattern Matching

  1. Customer names starting with 'A'.

SELECT * FROM Customers WHERE CustomerName LIKE 'A%';
  1. Products containing 'chocolate'.

SELECT * FROM Products WHERE ProductName LIKE '%chocolate%';
  1. Contact names ending in 'son'.

SELECT * FROM Customers WHERE ContactName LIKE '%son';
  1. City names with 5 characters.

SELECT * FROM Customers WHERE City LIKE '_____';
  1. Customers whose name includes two 'e's.

SELECT * FROM Customers WHERE CustomerName LIKE '%e%e%';

🔹 IN, NOT IN

  1. Customers from UK, USA, or France.

SELECT * FROM Customers WHERE Country IN ('UK', 'USA', 'France');
  1. Products in categories 1, 3, 5.

SELECT * FROM Products WHERE CategoryID IN (1, 3, 5);
  1. Employees not in titles 'Sales Rep', 'Manager'.

SELECT * FROM Employees WHERE Title NOT IN ('Sales Representative', 'Manager');
  1. Orders not to countries Canada, Brazil.

SELECT * FROM Orders WHERE ShipCountry NOT IN ('Canada', 'Brazil');
  1. Customers not from Berlin, Madrid, or Rome.

SELECT * FROM Customers WHERE City NOT IN ('Berlin', 'Madrid', 'Rome');

🔹 BETWEEN, NOT BETWEEN

  1. Products priced between 20 and 50.

SELECT * FROM Products WHERE UnitPrice BETWEEN 20 AND 50;
  1. Orders shipped between Jan and Mar 1997.

SELECT * FROM Orders WHERE ShippedDate BETWEEN '1997-01-01' AND '1997-03-31';
  1. Employees born between 1960 and 1970.

SELECT * FROM Employees WHERE BirthDate BETWEEN '1960-01-01' AND '1970-12-31';
  1. Products with stock not between 10 and 100.

SELECT * FROM Products WHERE UnitsInStock NOT BETWEEN 10 AND 100;
  1. Freight charges between 50 and 200.

SELECT * FROM Orders WHERE Freight BETWEEN 50 AND 200;

🔹 NULL and NOT NULL

  1. Customers without fax numbers.

SELECT * FROM Customers WHERE Fax IS NULL;
  1. Orders that have no shipped date yet.

SELECT * FROM Orders WHERE ShippedDate IS NULL;
  1. Employees with extension number.

SELECT * FROM Employees WHERE Extension IS NOT NULL;
  1. Products that have no reorder level set.

SELECT * FROM Products WHERE ReorderLevel IS NULL;
  1. Suppliers without homepages.

SELECT * FROM Suppliers WHERE HomePage IS NULL;

🔹 Date Functions with WHERE

  1. Orders placed in 1996.

SELECT * FROM Orders WHERE YEAR(OrderDate) = 1996;
  1. Orders shipped in December.

SELECT * FROM Orders WHERE MONTH(ShippedDate) = 12;
  1. Employees born in April.

SELECT * FROM Employees WHERE MONTH(BirthDate) = 4;
  1. Customers registered before 1990 (assuming a field).

SELECT * FROM Customers WHERE YEAR(RegistrationDate) < 1990;
  1. Orders on weekends (MySQL 8+).

SELECT * FROM Orders WHERE DAYOFWEEK(OrderDate) IN (1, 7);

🔹 Complex WHERE with AND/OR

  1. Products under $10 and in stock.

SELECT * FROM Products WHERE UnitPrice < 10 AND UnitsInStock > 0;
  1. Orders shipped to Germany or France in 1998.

SELECT * FROM Orders
WHERE YEAR(ShippedDate) = 1998 AND ShipCountry IN ('Germany', 'France');
  1. Customers from UK with fax.

SELECT * FROM Customers WHERE Country = 'UK' AND Fax IS NOT NULL;
  1. Orders to USA in Dec 1997.

SELECT * FROM Orders
WHERE ShipCountry = 'USA' AND OrderDate BETWEEN '1997-12-01' AND '1997-12-31';
  1. Employees not from USA hired after 1990.

SELECT * FROM Employees WHERE Country != 'USA' AND YEAR(HireDate) > 1990;

🔹 Field-Based Conditions

  1. Products where units in stock < reorder level.

SELECT * FROM Products WHERE UnitsInStock < ReorderLevel;
  1. Orders where freight > total price of products (if custom field exists).

SELECT * FROM Orders WHERE Freight > 100;
  1. Orders where required date < shipped date.

SELECT * FROM Orders WHERE RequiredDate < ShippedDate;
  1. Products with more units on order than in stock.

SELECT * FROM Products WHERE UnitsOnOrder > UnitsInStock;
  1. Employees with more than 10 years in company.

SELECT * FROM Employees WHERE YEAR(CURDATE()) - YEAR(HireDate) > 10;

🔹 Case-Specific Checks

  1. City names in lowercase.

SELECT * FROM Customers WHERE BINARY City = LOWER(City);
  1. Phone numbers that start with '('.

SELECT * FROM Customers WHERE Phone LIKE '(%';
  1. Company names that include numbers.

SELECT * FROM Customers WHERE CompanyName REGEXP '[0-9]';
  1. Postal codes of exact 5 digits.

SELECT * FROM Customers WHERE PostalCode REGEXP '^[0-9]{5}$';
  1. Employee title that starts with ‘Sales’.

SELECT * FROM Employees WHERE Title LIKE 'Sales%';

Post a Comment

Previous Post Next Post