DEV Community

Cover image for Another SQL cheat sheet
·ſ
·ſ

Posted on • Originally published at blog.julien-maury.dev

Another SQL cheat sheet

Disclaimer

Be aware it's not an exhaustive list. Click items with an arrow on the left to get details.

Concepts

Structured Query Language (SQL)
It's the most widely used language for managing data in relational databases. The language is easy to learn, declarative, and very convenient to manipulate datasets.

SQL for what?
SQL can be helpful to execute queries, create data schemes, manipulate records, and retrieve data. It's often used along with a programming language to power applications.

PostgreSQL
It's an open-source object-relational database management system (RDBMS).

MySQL
It's the most widely used relational database management system. It runs as a server, and you can use it to create databases and give their access to multiple users.

Columns vs. rows
In a database, All information is stored in tables. Each table is divided into rows and columns. Columns are specific attributes that allow for grouping data (e.g, id, first_name, last_name, age). Rows are records (e.g, 1001001, Kobe, Bryant, 37).

Views
A view is a virtual table based on the result of an SQL query. It's a pretty convenient feature that allow you to run advanced queries in pre-sorted results.

ACID vs. BASE
ACID means "atomicity, consistency, isolation, and durability", and BASE stands for “Basically Available, soft state and eventual consistency”. Each approach has its advantages and drawbacks depending on what you want to achieve.

NoSQL
The NoSQL model states that not all applications need a relational database. Redis, Elasticsearch or MongoDB are practical examples of NoSQL systems. You can read this post to understand the difference with SQL.

Logical operators

AND
Multiple conditions in WHERE clauses that are all verified.

OR
Multiple conditions in WHERE clauses where at least one condition is verified.

LIKE
Compare a value to similar values using wildcard operators.

BETWEEN
Search between max and min values.

EXISTS
Search if a row is in a table.

IN
Determine if a value is in a specified list of literal values.

NOT
Negate logical operator (e.g., NOT EXISTS, NOT BETWEEN, NOT IN, and so on).

Comparison Operators

=
Equal.

!=
Not equal.

<>
Not equal.

>
Greater than.

>=
Equal or greater than.

<
Lower than.

<=
Lower than or equal.

!>
Not greater than.

!<
Not lower than.

Basic commands

Create database
CREATE DATABASE my_database;
Enter fullscreen mode Exit fullscreen mode

Create table
CREATE TABLE my_table (
  my_column my_column_definition, 
  other_column other_column_definition, 
);
Enter fullscreen mode Exit fullscreen mode

Alter table
ALTER TABLE my_table 
ADD my_column my_column_definition;
Enter fullscreen mode Exit fullscreen mode

Empty table
TRUNCATE my_table;
Enter fullscreen mode Exit fullscreen mode

Select data in table
SELECT my_column 
FROM my_table;
Enter fullscreen mode Exit fullscreen mode

Select data conditionally
SELECT my_column, other_column
FROM my_table
WHERE my_column = some_value
AND other_column = other_value;
Enter fullscreen mode Exit fullscreen mode

Insert data in table
INSERT INTO my_table (my_column, other_column) 
VALUES (my_value, other_value);
Enter fullscreen mode Exit fullscreen mode

Update data in table
UPDATE my_table
SET my_column = my_value
WHERE other_column = other_value;
Enter fullscreen mode Exit fullscreen mode

Drop table
DROP my_table;
Enter fullscreen mode Exit fullscreen mode

Delete data table
DELETE FROM my_table
WHERE CONDITION;
Enter fullscreen mode Exit fullscreen mode

Aliasing
SELECT my_column AS 'mycol'
FROM my_table;
Enter fullscreen mode Exit fullscreen mode

Counting elements
SELECT COUNT(my_column)
FROM my_table;
Enter fullscreen mode Exit fullscreen mode

A little more advanced commands

Kill doublons in select
SELECT DISTINCT id FROM Players;
Enter fullscreen mode Exit fullscreen mode

Sum values in column
SELECT SUM(salary) FROM Players;
Enter fullscreen mode Exit fullscreen mode

Sort data
SELECT last_name, age FROM Players
GROUP BY age ORDER BY last_name;
Enter fullscreen mode Exit fullscreen mode

Not equal
SELECT first_name, last_name FROM Players WHERE age <> 42;
Enter fullscreen mode Exit fullscreen mode

Create view
CREATE VIEW my_view AS
SELECT id, first_name, last_name, age
FROM Players;
Enter fullscreen mode Exit fullscreen mode

Select data from a view
SELECT * FROM my_view;
Enter fullscreen mode Exit fullscreen mode

Drop view
DROP VIEW my_view;
Enter fullscreen mode Exit fullscreen mode

PRIMARY KEY constraints

A primary key is unique identifier for each records in a table. Its value cannot be NULL. It's often combined with AUTO_INCREMENT to generate it automatically:

CREATE TABLE `Players` (
   id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
   last_name varchar(255) NOT NULL,
   first_name varchar(255),
   age int,
);
Enter fullscreen mode Exit fullscreen mode

UNIQUE constraints

A UNIQUE constraint forces the use of different values in a column:

CREATE TABLE `Players` (
    id int NOT NULL,
    last_name varchar(255) NOT NULL,
    first_name varchar(255),
    age int,
    UNIQUE (id)
);
Enter fullscreen mode Exit fullscreen mode

INTERSECTION

SELECT * FROM table_A
INTERSECT
SELECT * FROM table_B
Enter fullscreen mode Exit fullscreen mode

INTERSECT allows you to get results that match the first query and the second query, and only that.

UNION

SELECT * FROM table_A
UNION
SELECT * FROM table_B
Enter fullscreen mode Exit fullscreen mode

UNION is a basic command that allows for combining several queries and concatenate results. In the example above, you get results that match the first query or the second query, or both.

JOIN

You can consolidate information from several different tables with a SQL JOIN:

INNER JOIN
Join two tables where a condition is true in the two tables:
SELECT *
FROM table_A
INNER JOIN table_B ON table_A.id = table_B.pid
Enter fullscreen mode Exit fullscreen mode

LEFT JOIN
Return all records from the table on the left even if the condition is not true in the second table:
SELECT *
FROM table_A
LEFT JOIN table_B ON table_A.id = table_B.pid;
Enter fullscreen mode Exit fullscreen mode

RIGHT JOIN
Return all records from the table on the right even if the condition is not true in the first table:
SELECT *
FROM table_A
RIGHT JOIN table_B ON table_A.id = table_B.pid;
Enter fullscreen mode Exit fullscreen mode

FULL JOIN and FULL OUTER JOIN
Join two tables where a condition is true at least in one table:
SELECT *
FROM table_A
FULL JOIN table_B ON table_A.id = table_B.pid;
Enter fullscreen mode Exit fullscreen mode

SELF JOIN
Join a table with itself. It's useful when there are hierarchical relationships between records:
SELECT child.id, child.name, parent.name
FROM People as child
LEFT OUTER JOIN People AS parent ON child.parent_id = parent.id
Enter fullscreen mode Exit fullscreen mode

CROSS JOIN
Join each line of a table with each line of a second table:
SELECT *
FROM table_A
CROSS JOIN table_B;
Enter fullscreen mode Exit fullscreen mode

NATURAL JOIN
Join two tables when they have at least one column with the same name:
SELECT *
FROM table_A
NATURAL JOIN table_B;
Enter fullscreen mode Exit fullscreen mode

UNION JOIN
Join union.

SQL Indexes

Indexes are incredibly helpful to decrease execution time for SQL selects and WHERE clauses.

It's pretty much like the index at the end of a book where you can find all pages related to a specific topic.

Creating (or dropping) indexes does not affect data:

CREATE INDEX my_index ON my_table;
Enter fullscreen mode Exit fullscreen mode

You can create an index on a specific column:

CREATE INDEX my_index ON my_table (my_column);
Enter fullscreen mode Exit fullscreen mode

Be careful, though, indexes are not meant for small tables or frequently updated columns. Large operations involving updates and inserts can be significantly slower.

Discussion (0)