SQL Code Snippets

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.

Add to Pieces

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.

Select

Tags: sql, select

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

SELECT * FROM table;

Related links:

  1. SQL select statement
  2. SQL select statement examples

Order By

Tags: sql, 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;

Related links:

  1. Order by syntax in SQL
  2. Order by clause in SQL

Select distinct

Tags: sql, select, distinct

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

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

Related links:

  1. Select distinct statement in SQL
  2. Select distinct clause in SQL
  3. Select distinct SQL example

AND

Tags: sql, and

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

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

Related links:

  1. AND operator in SQL
  2. AND, OR, NOT operators in SQL
  3. SQL, AND, OR NOT operators

OR

Tags: sql, 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 condition3 ...;

Related links:

  1. OR operator in SQL
  2. AND, OR, NOT operators in SQL
  3. SQL AND, OR, NOT operators

NOT

Tags: sql, 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;

Related links:

  1. NOT operator in SQL
  2. NOT keyword in SQL

Insert into

Tags: sql, insert into

This SQL code snippet inserts new records into a table.

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

Related links:

  1. SQL insert into statement
  2. How to use insert into command in SQL
  3. Insert query in SQL

Update

Tags: sql, 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;

Related links:

  1. Syntax for update in SQL
  2. Update keyword in SQL
  3. Update query in SQL

Delete

Tags: sql, delete

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

DELETE FROM table_name WHERE condition;

Related links:

  1. How to delete a record in SQL
  2. Delete statement in SQL
  3. SQL delete statement

Count

Tags: sql, 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;

Related links:

  1. SQL Count, Avg, and Sum functions
  2. Select count in SQL
  3. Count function in SQL

Avg

Tags: sql, average, avg

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

SELECT AVG(column_name)
FROM table_name
WHERE condition;

Related links:

  1. Avg function in SQL example
  2. SQL server Avg function
  3. SQL Count, Avg and Sum functions

Sum

Tags: sql, sum

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

SELECT SUM(column_name)
FROM table_name
WHERE condition;

Related links:

  1. Sum function in SQL
  2. SQL Sum function examples

Like

Tags: sql, 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 pattern;

Related links:

  1. Like operator in SQL
  2. Like clause in SQL

IN

Tags: sql, 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);

Related links:

  1. SQL IN Operator
  2. SQL IN Keyword
  3. SQL IN Condition

Between

Tags: sql, 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 value2;

Related links:

  1. Between keyword in SQL
  2. Between Operator in SQL server
  3. SQL Where Between

Inner Join

Tags: sql, 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 table1.column_name = table2.column_name;

Related links:

  1. How Inner Join works in SQL
  2. SQL Inner Join keyword
  3. SQL Inner Join example

Left Join

Tags: sql, 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 table1.column_name = table2.column_name;

Related links:

  1. SQL Left Join Keyword
  2. Left Join in SQL
  3. Left Join in SQL with example

Right Join

Tags: sql, 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 table1.column_name = table2.column_name;

Related links:

  1. SQL server right join
  2. SQL right join syntax
  3. Right Join in SQL

Full Join

Tags: sql, 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;

Related links:

  1. Full Join in SQL
  2. SQL Full Join example

Self Join

Tags: sql, 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;

Related links:

  1. SQL Self Join examples
  2. Self Join in SQL server
  3. Self Join in SQL

Group By

Tags: sql, 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);

Related links:

  1. Group By in SQL
  2. SQL Group By example

Union

Tags: sql, 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 table2;

Related links:

  1. Combine Queries in SQL
  2. Union usage in SQL
  3. Union operator in SQL example

Having

Tags: sql, 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);

Related links:

  1. SQL having clause
  2. SQL Server Having clause
  3. Having clause in SQL

Exists

Tags: sql, 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);

Related links:

  1. Exists in SQL with examples
  2. Exists condition in SQL
  3. Exists vs Not Exists SQL

Create DB

Tags: sql, database, create

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

CREATE DATABASE db;

Related links:

  1. How to create a DB in SQL
  2. How to create database in SQL server
  3. SQL create database statement

Create table

Tags: sql, table, create

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),
	....
);

Related links:

  1. SQL Create Table Statement
  2. Create a New Table in SQL server
  3. Create Table SQL statement

Want to use these SQL code snippets in your IDE? Download our IntelliJ plugin or VS Code extension to create your own SQL code snippet library and improve your developer productivity wherever you code.

Let us know what you think! Would you like to see other useful SQL code snippets not listed on this page? Suggest a collection you’d like to see to help other developers speed up their workflows.