Common MySQL Queries & Query Functions Cheatsheet

This is a free resource from my online course, From Idea To Launch, where I teach you how to build a full Laravel web application, step by step, at beginner's speed. Learn more →

A query is a command used to interact with a database. When you interact with a MySQL database, queries are written in the SQL language.

Below are all of the common MySQL query commands as well as examples of common queries.


Common query functions

  • SELECT — used to retrieve objects from a database table
  • INSERT — used to insert new objects into a database table
  • UPDATE — used to update existing objects in a database table
  • DELETE — used to delete objects from a database table
  • FROM — used to specify the database table you wish to interact with
  • WHERE — used to filter the objects retrieved based on specified criteria
  • LIMIT — used to restrict the number of objects retrieved via a query
  • LIKE — used to perform string comparisons; the % wildcard matches any number of characters, even zero characters; e.g. to select animals whose type contains “wolf”, you could use SELECT * FROM animals WHERE type LIKE '%wolf%'

Queries to Select, Insert, Update, and Delete objects in a MySQL database

Say, for example, you have a database table, cars, with the following columns:

  • make — a VARCHAR column with the brand/company that made the car
  • model — a VARCHAR column with the car’s model
  • year — a YEAR column with the year the car was made

Selecting objects:


/* select all cars */
SELECT * FROM cars;

/* select only the model and year of all cars */
SELECT model, year FROM cars;

/* select all Chevys */
SELECT * FROM cars WHERE make = 'Chevy';

/* select all Corvette and Corvette variations */
SELECT * FROM cars WHERE model LIKE '%corvette%';
                                         
/* select the first 10 Chevys */
SELECT * FROM cars WHERE make = 'Chevy' LIMIT 10;

Inserting an object:


/* create a new 1969 Chevy Corvette */
INSERT INTO cars
(make, model, year)
VALUES
('Chevrolet', 'Corvette', 1969);

Updating an object:


/* update the model of all 1969 Chevys to "Corvette Stingray"  */
UPDATE cars
SET model = 'Corvette Stingray'
WHERE make = 'Chevrolet' AND year = 1969;

Deleting objects:


/* delete all Chevys */
DELETE FROM cars
WHERE make = 'Chevrolet';