5 MINUTES

OF MYSQL

Step 1

SELECT * FROM customers;

Try Running This!

SELECT * FROM orders;

Then Try Running This!

SELECT * 
FROM products
ORDER BY Price DESC;

More Code!

SELECT 
    customerName,
    COUNT(*) AS 'number of orders'
FROM customers
INNER JOIN orders
	ON orders.CustomerID = customers.CustomerID
GROUP BY customers.customerID
ORDER BY "number of orders" DESC;

Something a bit more complex

SELECT 
    username,
    photos.id,
    photos.image_url, 
    COUNT(*) AS total
FROM photos
INNER JOIN likes
    ON likes.photo_id = photos.id
INNER JOIN users
    ON photos.user_id = users.id
GROUP BY photos.id
ORDER BY total DESC
LIMIT 1;

Where We're Going

SELECT first_name, 
       last_name, 
       Count(rating)                    AS COUNT, 
       Ifnull(Min(rating), 0)           AS MIN, 
       Ifnull(Max(rating), 0)           AS MAX, 
       Round(Ifnull(Avg(rating), 0), 2) AS AVG, 
       CASE 
         WHEN Count(rating) >= 10 THEN 'POWER USER' 
         WHEN Count(rating) > 0 THEN 'ACTIVE' 
         ELSE 'INACTIVE' 
       end                              AS STATUS 
FROM   reviewers 
       LEFT JOIN reviews 
              ON reviewers.id = reviews.reviewer_id 
GROUP  BY reviewers.id; 

Or This:

DROP TABLE customers;

Try Running This!

👋 goodbye, customers

MySQL 2.0: First 5 Minutes Of SQL

By Colt Steele

MySQL 2.0: First 5 Minutes Of SQL

  • 38,704