Skip to main content

The Essential SQL Commands Cheat Sheet for Beginners

MacBook with code displayed on screen

Structured Query Language (SQL) is the tool of choice for manipulating databases. This language lets you manipulate data like no other, and it costs nothing to start using!

If you’re new to SQL commands or need a reminder of some parts of this powerful tool at your disposal, then our SQL commands cheat sheet below is exactly what you need. Scroll down to see the awesomeness it contains, or download the PDF for future use. (The PDF contains examples for the given commands.)

FREE DOWNLOAD: This cheat sheet is available as a downloadable PDF from our distribution partner, TradePub. You will have to complete a short form to access it for the first time only. Download The Essential SQL Commands Cheat Sheet.

The Essential SQL Commands Cheat Sheet

Command Action
Query Commands
SELECT Basic query building block to retrieve data.
SELECT * Using * with SELECT returns all columns.
SELECT column Specify exact columns with their name.
SELECT table.column Reference a column from a specific table.
FROM Specify where to find data.
AS Temporarily alias a table name or column to a new name.
WHERE Filter results with a condition.
AND Use multiple conditions with a WHERE clause. Results must match all conditions.
OR Use multiple conditions with a WHERE clause. Results only need to match one condition.
ORDER BY Order the results by a column. The database chooses how to order.
ORDER BY column ASC Order the results by a column in ascending order.
ORDER BY column DESC Order the results by a column in descending order.
LIMIT Restrict the number of results returned.
OFFSET Skip the first OFFSET number of rows. Often used with LIMIT.
SUBQUERY Run a query to retrieve data for another query.
Aggregate Functions¹
COUNT Count the number of rows that match the query.
MAX Return the highest value in a numeric column.
MIN Return the lowest value in a numeric column.
SUM Sum the values of a numeric column.
AVG Calculate the average value for a numeric column.
HAVING Used with aggregate functions instead of the WHERE clause.
GROUP BY Used to refine an aggregate result.
Operators
LIKE Case-sensitive search for a pattern with a wildcard operator (%).
ILIKE Case-insensitive search for a pattern with a wildcard operator (%).
BETWEEN Search for a value between two values. Works with dates or numbers.
> Search for values greater than a condition.
>= Search for values greater or equal to a condition.
< Search for values less than a condition.
<= Search for values less than or equal to a condition.
= Search for values matching a condition exactly.
<> Search for values not equal to a condition.
UNION Combine two unique queries (with the same columns) into one result.
UNION ALL Combine two queries (with the same columns) into one result. Duplicates allowed.
IN Shorthand for WHERE. Specifies multiple OR conditions.
NOT IN Shorthand for WHERE. Specifies multiple OR conditions (inverted) or not equal to.
IS NULL Check for empty values.
IS NOT NULL Check for no empty values.
INTERSECT Return results which match two queries.
MINUS Return results in one query which are not in another query.¹
Joins
ON Used to specify the column to compare and match results.
USING Shorthand for ON, used when the column name is the same in both tables.
LEFT OUTER JOIN All the results from the left table, with only the matching results from the right table.
LEFT OUTER JOIN (WITH NULL) (With null) All the results from the left table but not in the right table.
INNER JOIN All the results that match in both the left and right tables.
FULL OUTER JOIN All the results from both the left and right tables.
FULL OUTER JOIN (WITH NULL) (With null) all the results from both the left and right tables excluding results in both tables.
RIGHT OUTER JOIN All the results from the right table, with only the matching results from the left table.
RIGHT OUTER JOIN (WITH NULL) (With null) All the results from the right table but not in the left table.
Creating and Editing Tables
CREATE TABLE Create a new table.
NULL Allow empty values for this field.
NOT NULL Don't allow empty values for this field.
DEFAULT A value to populate the field with if one is not supplied.
AS Create a new table based on the structure of an existing table. The new table will contain the data from the old table.
ALTER TABLE (ADD COLUMN) Add a new column to an existing table.
ALTER TABLE (DROP COLUMN) Remove a column from an existing table.
ALTER TABLE (ALTER COLUMN) Change the datatype of an existing column.
ALTER TABLE (RENAME COLUMN) Rename an existing column.
ALTER TABLE (RENAME TABLE) Rename an existing table.
ALTER TABLE (MODIFY NULL) Allow null values for a column.
ALTER TABLE (MODIFY NOT NULL) Prevent null values for a column.
DROP TABLE Delete a table and all its data.
TRUNCATE TABLE Delete all the data in a table, but not the table itself.
Constraints
PRIMARY KEY A value that uniquely identifies a record in a table. A combination of NOT NULL and UNIQUE.
FOREIGN KEY References a unique value in another table. Often a primary key in the other table.
UNIQUE Enforce unique values for this column per table.
CHECK Ensure values meet a specific condition.
INDEX (CREATE) Optimise tables and greatly speed up queries by adding an index to a column.
INDEX (CREATE UNIQUE) Create an index that does not allow duplicate values.
INDEX (DROP) Remove an index.
Creating and Editing Data
INSERT (SINGLE VALUE) Add a new record to a table.
INSERT (MULTIPLE VALUES) Add several new records to a table.
INSERT (SELECT) Add records to a table, but get the values from an existing table.
UPDATE (ALL) Modify all existing records in a table.
UPDATE (WHERE) Modify existing records in a table which match a condition.
DELETE (ALL) Remove all records from a table.
DELETE (WHERE) Remove records from a table which match a condition.
Creating and Editing Triggers¹
CREATE TRIGGER Create a trigger.
CREATE TRIGGER (OR MODIFY) Create a trigger, or update an existing trigger if one is found with the same name.
WHEN (BEFORE) Run the trigger before the event happens.
WHEN (AFTER) Run the trigger after the event happens.
EVENT (INSERT) Run the trigger before or after an insert happens.
EVENT (UPDATE) Run the trigger before or after an update happens.
EVENT (DELETE) Run the trigger before or after a delete happens.
ON Which table to target with this trigger.
TRIGGER_TYPE (FOR EACH ROW) Execute the trigger for every row changed.
TRIGGER_TYPE (FOR EACH STATEMENT) Execute the trigger once per SQL statement, regardless of how many rows are altered.
EXECUTE Keyword to indicate the end of the main trigger definition.
DROP TRIGGER Delete a trigger.
Creating and Editing Views
CREATE VIEW Create a new view.
AS Define where to retrieve the data for a view.
WITH CASCADED CHECK OPTION Ensure any data modified through a view meets the rules defined by the rule. Apply this to any other views.
WITH LOCAL CHECK OPTION Ensure any data modified through a view meets the rules defined by the rule. Ignore this for any other views.
CREATE RECURSIVE VIEW Create a recursive view (one that refers to a recursive common table expression).
CREATE TEMPORARY VIEW Create a view that exists for the current session only.
DROP VIEW Delete a view.
Common Table Expressions (CTEs)¹
WITH Create a new common table expression.
AS Specify the data to use in the CTE.
, (COMMA) Chain multiple CTEs.
¹Database engine implementations and support often vary.

SQL: The Most Powerful Language Around?

Keep in mind that SQL dialects vary between database engines. It’s a bit like the difference between HD DVD and Blu-ray (or VHS and Betamax). SQL is similar between databases, but the occasional complex command may not work exactly the same across all implementations. Most of the SQL commands in this cheat sheet will work across any database. The complex commands where database support varies are noted as such.

Once you know SQL, you can apply it to a variety of uses. Whether you’re dealing with a management system, programming project, or basic reporting, SQL unlocks the raw power contained in a database. Don’t forget to read our basic programmer’s guide to SQL, for an in-depth look at how to use these commands.

Read the full article: The Essential SQL Commands Cheat Sheet for Beginners

Comments

Popular posts from this blog

The Best 10 Social Media Platforms for Photographers to Flaunt Their Talent

Social media offers an excellent opportunity for photographers to connect with potential clients. In the digital era, it's a great asset. By showcasing your work on these networks, you can reach new audiences. Whether you are a professional or freelance photographer, the following social platforms will help you show off your work and get the right people to take notice... 1. Behance Behance is a classic portfolio publishing network that functions like a LinkedIn for creatives. Designed by Adobe, this is one of the best photography networking sites currently out there. The platform is ideal for sharing your portfolio and favorite images, allowing other Behance users to like and comment on your photos. By learning from their feedback and professional critiques, you can improve your work. The coolest feature of Behance is that it lets you find professional gig opportunities right on the platform. With your portfolio already available on the site, getting work becomes effortless. ...

The 6 Best Platforms for Sharing Your Digital Art Online

Whether you're looking for somewhere to host your digital art portfolio or simply want to share your latest artworks, it can be difficult to choose a website to upload to. Or at least, it definitely is more so than before, now that art websites aren't bubbling with as much excitement as they used to be. You know that each site has its pros and cons, but it's hard to figure out what those are unless you make an account and see for yourself. Don't worry if you don't have time for that—we've got your back. Here are the websites we recommend for sharing digital art, and why you might want to consider them. 1. Pixiv If you were around when the online art scene was ridiculously active, chances are that your art style is influenced by anime and/or manga in some way. Otaku culture began its slow sneak into mainstream media back then, and Pixiv is a great home for artists that fall in that category. Pixiv started as a small online community based in Japan,...

Snapchat Suspends Two Anonymous Messaging Apps Over Cyberbullying Claims

In light of a lawsuit that was filed earlier, two Snapchat apps, Yolo and LMK have been suspended by Snap. The apps allowed users to send anonymous messages on the platform. The Lawsuit Calls for an Immediate Ban of Yolo and LMK According to a LA Times report, the lawsuit was filed on behalf of Kristin Bride, the mother of a teen who committed suicide in 2020. The lawsuit alleges that Bride's son took his own life after being cyberbullied via Yolo and LMK. In addition to this, the lawsuit alleges that Yolo and LMK aren't doing enough to tackle cyberbullying, and have consequently violated consumer protection law as well as their own terms of service and policies. Both apps use Snap Kit, a set of tools that allows developers to directly connect to Snapchat for better integration features. Today the family of a 16-year-old Oregon boy who took his own life after being cyberbullied sued Snap and the makers of apps YOLO and LMK, alleging that the companies should be "h...