SQL

This collection of SQL code snippets ranges from Select to Exist. Find the data you need as quickly as possible with this set of super useful SQL code snippets.

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

When adding any of the following SQL developer snippets to Pieces, developers will receive relevant tags for easy and fast search in Pieces, a description for context, and related links.

Tags: sql, select

Select

Use the select SQL command to select data from a table.

SELECT * FROM table

Tags: sql, order by

Order By

Use the SQL order by command to sort the result-set in ascending or descending order.

SELECT * FROM table_name ORDER BY column DESC

Tags: sql, select, distinct

Select distinct

The select distinct command in SQL is used to return only distinct values.

SELECT DISTINCT column1, column2, ...
FROM

Tags: sql, and

AND

This sample SQL code displays a record if all the conditions separated by AND are true.

SELECT DISTINCT column1, column2, ...
FROM

Tags: sql, or

OR

The OR command in SQL displays a record if any of the conditions separated by OR is TRUE.

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

Tags: sql, not

NOT

This NOT SQL sample code snippet displays a record if the condition(s) is NOT TRUE.

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

Tags: sql, insert into

Insert into

This SQL code snippet inserts new records into a table.

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

Tags: sql, update

Update

The Update command in SQL is used to modify the existing records in a table.

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

Tags: sql, delete

Delete

The Delete command in SQL is used to delete existing records in a table.

DELETE FROM table_name WHERE condition

Tags: sql, count

Count

The Count command in SQL returns the number of rows that matches a specified criterion.

SELECT COUNT(column_name)
FROM table_name
WHERE condition

Tags: sql, average, avg

Avg

The Avg command in SQL returns the average value of a numeric column.

SELECT AVG(column_name)
FROM table_name
WHERE condition

Tags: sql, sum

Sum

The Sum command in SQL returns the total sum of a numeric column.

SELECT SUM(column_name)
FROM table_name
WHERE condition

Tags: sql, like

Like

The Like command in SQL is used to search for a specified pattern in a column.

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

Tags: sql, in

IN

The IN command in SQL allows you to specify multiple values in a WHERE clause.

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

Tags: sql, between

Between

The Between command in SQL selects values within a given range.

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

Tags: sql, inner join

Inner Join

The Inner Join Command in SQL selects records that have matching values in both tables.

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON

Tags: sql, left join

Left Join

The Left Join command in SQL 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

Tags: sql, right join

Right Join

The right join command in SQL 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

Tags: sql, full join

Full Join

The Full Join SQL command 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

Tags: sql, self join

Self Join

The Self Join SQL command is a regular join but the table is joined with itself.

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

Tags: sql, group by

Group By

The Group By command in SQL 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)

Tags: sql, union

Union

The Union command in SQL is used to combine the result-set of two or more SELECT statements.

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

Tags: sql, having

Having

The Having command in SQL 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)

Tags: sql, exists

Exists

The Exists Command in SQL 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)

Tags: sql, database, create

Create DB

Use the SQL create DB Command to create a new SQL database.

CREATE

Tags: sql, table, create

Create table

Use this SQL snippet to create a new table.

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

150,000+ Installs

Want to use these SQL code snippets in your IDE?

Install one of our IDE integrations to 10x your productivity during your next coding project.