Blog

MySQL Queries Cheat Sheet
Posted on December 31, 2017 in MySQL by Matt Jennings

Knowledge gained from Learn SQL and MySQL in 3 Hours by Umang Shah (Udemy class).

Starting Database and Tables

super_market (database and tables below)

  • employees
  • items
  • sales (have foreign keys from employees and items tables)

Display all Rows in the employees Table

SELECT * FROM employees;

List of First 5 Employees Using WHERE

SELECT *
FROM employees
WHERE employeeId <= 5;

Get Only the name Column of the employees Table

SELECT name
FROM employees;

Get Names of Employees Who employee_id is Either 1, 2, or 4 using IN

SELECT *
FROM employees
WHERE employee_id IN (1, 2, 4);

Get List of Items in Descending Order (default is Ascending) by item_id

SELECT *
FROM items
ORDER BY item_id DESC;

Get List of Employee Names that Start with the Letter “D”

SELECT *
FROM employees
WHERE name LIKE 'd%';

Get List of All Employees’ Names that Do NOT Start with the Letter “D”

SELECT *
FROM employees
WHERE name NOT LIKE 'd%';

Left Join items Table with sales Table

SELECT *
FROM items
LEFT JOIN sales ON items.item_id = sales.item_id;

Inner Join items Table with sales Table

*Note: An INNER JOIN means that only rows with values will return.

SELECT *
FROM items
INNER JOIN sales ON items.items_id = sales.items_id;

Leave a Reply