SQL

Find the data you need as quickly as possible with this set of super useful SQL commands.

Add to Pieces

SQL has a lot of repetitive statements, and with this collection, you will be able to grab many common SQL operations to make your ability to grab data even quicker!

Select

Tags: sql, select

Used to select data from a table.

SELECT * FROM table;

Related links:

  1. https://www.w3schools.com/sql/sql_select.asp
  2. https://www.techonthenet.com/sql/select.php

Order By

Tags: sql, order by

Used to sort the result-set in ascending or descending order.

SELECT * FROM table_name ORDER BY column DESC;

Related links:

  1. https://www.freecodecamp.org/news/ascending-order-with-sql-order-by/
  2. https://www.tutorialspoint.com/sql/sql-order-by.htm

Select distinct

Tags: sql, select , distinct

Used to return only distinct values.

SELECT DISTINCT column1, column2, ...
FROM table_name;

Related links:

  1. https://www.w3schools.com/sql/sql_distinct.asp
  2. https://www.sqlservertutorial.net/sql-server-basics/sql-server-select-distinct/
  3. https://www.mssqltips.com/sqlservertip/6810/sql-select-distinct-examples/

And

Tags: sql, and

Displays a record if all the conditions separated by AND are true.

SELECT DISTINCT column1, column2, ...
FROM table_name;

Related links:

  1. https://mode.com/sql-tutorial/sql-and-operator/
  2. https://www.w3schools.com/sql/sql_and_or.asp
  3. https://www.geeksforgeeks.org/sql-and-and-or-operators/

Or

Tags: sql, or

Displays a record if any of the conditions separated by OR is TRUE.

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

Related links:

  1. https://mode.com/sql-tutorial/sql-or-operator/
  2. https://www.w3schools.com/sql/sql_and_or.asp
  3. https://www.geeksforgeeks.org/sql-and-and-or-operators/

Not

Tags: sql, not

Displays a record if the condition(s) is NOT TRUE.

SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

Related links:

  1. https://mode.com/sql-tutorial/sql-not-operator/
  2. https://www.w3schools.com/sql/sql_ref_not.asp

Insert into

Tags: sql, insert into

Inserts new records into a table.

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Related links:

  1. https://www.w3schools.com/mysql/mysql_insert.asp
  2. https://www.freecodecamp.org/news/insert-into-sql-how-to-insert-into-a-table-query-example-statement/
  3. https://www.tutorialspoint.com/sql/sql-insert-query.htm

Update

Tags: sql, update

Used to modify the existing records in a table

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Related links:

  1. https://www.simplilearn.com/tutorials/sql-tutorial/sql-update
  2. https://www.w3schools.com/sql/sql_ref_update.asp
  3. https://www.tutorialspoint.com/sql/sql-update-query.htm

Delete

Tags: sql, delete

Used to delete existing records in a table.

DELETE FROM table_name WHERE condition;

Related links:

  1. https://www.simplilearn.com/tutorials/sql-tutorial/delete-in-sql
  2. https://www.techonthenet.com/sql/delete.php
  3. https://www.zentut.com/sql-tutorial/sql-delete/

Count

Tags: sql, count

Returns the number of rows that matches a specified criterion.

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

Related links:

  1. https://www.w3schools.com/sql/sql_count_avg_sum.asp
  2. https://www.javatpoint.com/sql-select-count
  3. https://www.techonthenet.com/sql/count.php

Avg

Tags: sql, average, avg

Returns the average value of a numeric column.

SELECT AVG(column_name)
FROM table_name
WHERE condition;

Related links:

  1. https://www.sqlshack.com/sql-avg-function-introduction-and-examples/
  2. https://www.sqlservertutorial.net/sql-server-aggregate-functions/sql-server-avg/
  3. https://www.w3schools.com/sql/sql_count_avg_sum.asp

Sum

Tags: sql, sum

Returns the total sum of a numeric column.

SELECT SUM(column_name)
FROM table_name
WHERE condition;

Related links:

  1. https://www.tutorialspoint.com/sql/sql-sum-function.htm
  2. https://learnsql.com/blog/sql-sum-function-explained/

Like

Tags: sql, like

Used to search for a specified pattern in a column.

SELECT column1, column2, ...
FROM table_name
WHERE column LIKE pattern;

Related links:

  1. https://www.w3schools.com/sql/sql_like.asp
  2. https://www.tutorialspoint.com/sql/sql-like-clause.htm

In

Tags: sql, in

Allows you to specify multiple values in a WHERE clause.

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

Related links:

  1. https://www.mssqltips.com/sqlservertip/6858/sql-in-operator/
  2. https://www.w3schools.com/sql/sql_ref_in.asp
  3. https://www.techonthenet.com/sql/in.php

Between

Tags: sql, between

Selects values within a given range.

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Related links:

  1. https://www.w3schools.com/sql/sql_ref_between.asp
  2. https://www.sqlservertutorial.net/sql-server-basics/sql-server-between/
  3. https://www.dofactory.com/sql/where-between

Inner Join

Tags: sql, inner join

Selects records that have matching values in both tables.

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

Related links:

  1. https://www.sqltutorial.org/sql-inner-join/
  2. https://www.w3schools.com/sql/sql_join_inner.asp
  3. https://www.sqlshack.com/a-step-by-step-walkthrough-of-sql-inner-join/

Left Join

Tags: sql, left join

Returns all records from the left table and the matching records from the right table. The result is no records from the right side if there is not a match.

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Related links:

  1. https://www.w3schools.com/sql/sql_ref_left_join.asp
  2. https://www.tutorialspoint.com/sql/sql-left-joins.htm
  3. https://www.geeksforgeeks.org/sql-left-join/

Right Join

Tags: sql, right join

Returns all records from the right table and the matching records from the left table. The result is no records from the left side if there is not a match.

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

Related links:

  1. https://www.sqlservertutorial.net/sql-server-basics/sql-server-right-join/
  2. https://www.geeksforgeeks.org/sql-right-join/
  3. https://www.tutorialspoint.com/sql/sql-right-joins.htm

Full Join

Tags: sql, full join

Returns all records when there is a match in left or right table records.

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

Related links:

  1. https://www.dofactory.com/sql/full-join
  2. https://www.tutorialspoint.com/sql/sql-full-joins.htm

Self Join

Tags: sql, self join

A regular join but the table is joined with itself.

SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

Related links:

  1. https://learnsql.com/blog/what-is-self-join-sql/
  2. https://www.sqlservertutorial.net/sql-server-basics/sql-server-self-join/
  3. https://www.dofactory.com/sql/self-join

Group By

Tags: sql, group by

Groups rows that have the same values into summary rows.

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

Related links:

  1. https://www.programiz.com/sql/group-by
  2. https://www.tutorialspoint.com/sql/sql-group-by.htm

Union

Tags: sql, union

Used to combine the result-set of two or more SELECT statements.

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

Related links:

  1. https://www.simplilearn.com/tutorials/sql-tutorial/sql-union
  2. https://www.sqlshack.com/sql-union-overview-usage-and-examples/
  3. https://www.techonthenet.com/sql/union.php

Having

Tags: sql, having

Specifies conditions that filter which group results appear in the results.

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

Related links:

  1. https://www.w3schools.com/sql/sql_having.asp
  2. https://www.sqlservertutorial.net/sql-server-basics/sql-server-having/
  3. https://www.techonthenet.com/sql/having.php

Exists

Tags: sql, exists

Tests for the existence of any record in a subquery.

SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);

Related links:

  1. https://www.geeksforgeeks.org/sql-exists/
  2. https://www.techonthenet.com/sql/exists.php
  3. https://blog.devart.com/sql-exists-and-not-exists.html

Create DB

Tags: sql, database, create

Used to create a new SQL database.

CREATE DATABASE db;

Related links:

  1. https://www.sqlshack.com/learn-sql-create-database-create-table/
  2. https://www.sqlservertutorial.net/sql-server-basics/sql-server-create-database/
  3. https://www.w3schools.com/sql/sql_create_db.asp

Create table

Tags: sql, table, create

Used to create a new table.

CREATE TABLE table_name
(
	column_name1 data_type(size),
	column_name2 data_type(size),
	column_name3 data_type(size),
	....
);

Related links:

  1. https://www.w3schools.com/sql/sql_create_table.asp
  2. https://www.tutorialsteacher.com/sqlserver/create-table
  3. https://www.programiz.com/sql/create-table