List of SQL Commands
List of SQL Commands
AND CONSTRAINTS
The Complete List from A to Z - BA Helpline
A
AND - The AND operator displays a record if all the conditions
separated by AND are TRUE. SELECT column1, column2, column3, ...
FROM table_name WHERE condition1 AND condition2 AND condition3.
ADD - Adds a column in an existing table.
ALTER TABLE People ADD Email varchar(255);
ADD CONSTRAINT - The ADD CONSTRAINT command is used to create
a constraint after a table is already created.
ALTER TABLE Persons ADD CONSTRAINT PK_Person PRIMARY KEY
(ID,LastName);
ANY – It means that the condition will be true if the operation is true
for any of the values in the range. It returns a boolean value as a result.
SELECT column_name(s) FROM table_name WHERE column_name
operator ANY (SELECT column_name FROM table_name WHERE
condition);
ALL – It means that the condition will be true only if the operation is
true for all values in the range. It is used with SELECT, WHERE and
HAVING statements and returns boolean value as a result.
SELECT column_name(s) FROM table_name WHERE column_name
operator ALL (SELECT column_name FROM table_name WHERE
condition);
Auto-increment – It allows a unique number to be generated
automatically when a new record is inserted into a table.
CREATE TABLE company (EmployeeID int NOT NULL AUTO_INCREMENT,
Last_Name varchar (255) NOT NULL, First_Name varchar (255), Age int,
PRIMARY KEY (EmployeeId));
ALTER TABLE - This statement is used to add, delete, or modify columns
in an existing table. It is also used to add and drop various constraints
on an existing table.
ALTER TABLE people ADD email_id varchar(255)
ALTER COLUMN - The ALTER COLUMN command is used to change the
data type of a column in a table.
ALTER TABLE People
ALTER COLUMN BirthDate year;
ASC - The ASC command is used to sort the data returned in ascending
order.
Select * from people order by age ASC;
AVG - AVG returns the average value of a numeric column.
Select AVG(quiz_points) from people;
AS - AS renames a column or table with an alias that we can choose. For
example, in the code below, we’re renaming the name column as
first_name:
Select name as first_name from people;
B
BETWEEN - The BETWEEN keyword in SQL is used in a WHERE clause to
specify a value must be within a specified range of values.
SELECT age FROM people WHERE age BETWEEN 100 AND 200;
BACKUP DATABASE - The BACKUP DATABASE statement is used in SQL
Server to create a full back up of an existing SQL database.
BACKUP DATABASE databasename TO DISK = 'filepath';
D
DROP DATABASE - This command will remove the specified database
from the system, and all objects included in the database.
DROP DATABASE database_name;
DROP INDEX - This command is used to remove an index from the
database.
DROP INDEX index_name;
DROP TABLE – This command is used to remove a table from the
database.
DROP TABLE table_name;
DROP VIEW - This command will remove or drop a view from the
system.
DROP VIEW states_people;
DROP CONSTRAINTS - The DROP CONSTRAINT command is used to
delete a UNIQUE, PRIMARY KEY, FOREIGN KEY, or CHECK constraint.
ALTER TABLE Persons
DROP CONSTRAINT UC_Person;
DROP COLUMN - The DROP COLUMN command is used to delete a
column in an existing table.
ALTER TABLE People
DROP COLUMN Age;
DROP DEFAULT - The DROP DEFAULT command is used to delete a
DEFAULT constraint.
ALTER TABLE People
ALTER COLUMN Age DROP DEFAULT;
DELETE - The DELETE keyword is used to delete or remove a record
from a table.
DELETE FROM people WHERE row_id = 1000;
DISTINCT - This keyword is used in SELECT statements to remove
duplicate rows from the result. If a row is duplicated in the results of
the SELECT query, adding DISTINCT will ensure only one is shown and
duplicates are not shown. It must be added immediately after the
SELECT keyword.
SELECT DISTINCT first_name, last_name FROM people;
DEFAULT - The DEFAULT constraint is used to set a default value for a
column. The default value will be added to all new records, if no other
value is specified.
CREATE TABLE People (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
quiz_points int,
City varchar(255) DEFAULT 'Mumbai'
);
DESC - The DESC command is used to sort the data returned in
descending order.
Select * from people order by age DESC;
E
EXISTS - This keyword allows you to check if a record is found within a
subquery. If it is found. it can be returned to the main query. It can be
used where subqueries are used, such as SELECT, INSERT, UPDATE, and
DELETE statements.
SELECT first_name
FROM people
WHERE EXISTS (SELECT state_abbrev FROM states WHERE stateid =
people.stateid AND age > 20);
EXEC - The EXEC command is used to execute a stored procedure.
EXEC SelectAllCustomers;
F
FROM - The FROM command is used to specify which table to select or
delete data from.
Select * from people;
FOREIGN KEY - The FOREIGN KEY constraint is a key used to link two
tables together.
CREATE TABLE People (
peopleid int NOT NULL,
age int NOT NULL,
stateid int,
PRIMARY KEY (peopleid),
FOREIGN KEY (stateid) REFERENCES state(stateid)
);
FULL OUTER JOIN - The FULL OUTER JOIN command returns all rows
when there is a match in either left table or right table.
SELECT order.order_id,
customer.first_name
FROM customer
FULL OUTER JOIN order ON customer.customer_id =
order.customer_id;
G
GROUP BY - The GROUP BY SQL command is used in a SELECT query to
combine or aggregate data into groups.
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
H
HAVING - The HAVING command is used along with a GROUP BY
command to filter the results of a SELECT query after it has been
grouped. It’s similar to WHERE, but WHERE works before the grouping
and HAVING works after the grouping.
SELECT first_name, COUNT(*)
FROM customer
GROUP BY first_name
HAVING COUNT(*) > 1;
I
IN - The IN command allows you to specify multiple values in a WHERE
clause.
SELECT * FROM people
WHERE first_name IN (‘Raj’, ‘Rahul’, ‘Bob’);
INNER JOIN - The INNER JOIN command returns rows that have
matching values in both tables.
SELECT order.order_id,
customer.first_name
FROM customer
INNER JOIN order ON customer.customer_id = order.customer_id;
INSERT INTO - The INSERT INTO command is used to insert new rows in
a table.
INSERT INTO people (first_name, age)
VALUES ('Rahul’, ‘21');
IS NULL - The IS NULL command is used to test for empty values (NULL
values).
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
IS NOT NULL - The IS NOT NULL command is used to test for non-empty
values (NOT NULL values).
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;
J
JOIN - In SQL, the CROSS JOIN is used to combine each row of the first
table with each row of the second table. It is also known as the
Cartesian join since it returns the Cartesian product of the sets of rows
from the joined tables.
Select * from people JOIN states;
L
LIKE - The LIKE command is used in a WHERE clause to search for a
specified pattern in a column.
Select * from people where first_name like ‘J%’;
LIMIT – It specify the number of records to return from the top;
Select * from people LIMIT 50;
LEFT JOIN - A Left Join, which can also be called a Left Outer Join, is a
type of join where two tables are joined together. However, the table
on the left of the JOIN keyword has all records shown, and if there are
any matches in the table on the right, they are also shown, otherwise
NULL values are shown.
SELECT order.order_id,
customer.first_name
FROM customer
LEFT JOIN order ON customer.customer_id = order.customer_id
N
NOT - The NOT command is used with WHERE to only include rows
where a condition is not true.
SELECT * FROM Customers
WHERE NOT Country='Germany';
NOT NULL - The NOT NULL constraint enforces a column to not accept
NULL values, which means that you cannot insert or update a record
without adding a value to this field.
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
O
OR - The OR command is used with WHERE to include rows where
either condition is true.
Select * from people where state_abbrev = ‘CA’ or age > 20;
ORDER BY - The ORDER BY command is used to sort the result set in
ascending or descending order.
Select * from people order by age;
OFFSET - The OFFSET clause specifies the number of rows to skip before
starting to return rows from the query.
Select * from people limit 50 OFFSET 10;
P
PRIMARY KEY - The PRIMARY KEY constraint uniquely identifies each
record in a table.
CREATE TABLE People (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
R
RIGHT JOIN - A Right Join, which can also be called a Right Outer Join, is
a type of join where two tables are joined together. However, the table
on the right of the JOIN keyword has all records shown, and if there are
any matches in the table on the left, they are also shown, otherwise
NULL values are shown.
SELECT order.order_id,
customer.first_name
FROM customer
RIGHT JOIN order ON customer.customer_id = order.customer_id;
ROWNUM - ROW_NUMBER function is a SQL ranking function that
assigns a sequential rank number to each new record in a partition.
When the SQL Server ROW NUMBER function detects two identical
values in the same partition, it assigns different rank numbers to both.
SELECT * FROM Customers
WHERE ROWNUM <= 3;
S
SELECT - The SELECT command is used to read data from the database.
It specifies the columns to be displayed, as well as any expressions or
calculations to be displayed.
Select first_name, last_name from people;
SELECT INTO - The SELECT INTO command copies data from one table
and inserts it into a new table.
SELECT * INTO CustomersBackup2017
FROM Customers;
SET - The SET command is used with UPDATE to specify which columns
and values that should be updated in a table.
UPDATE people
SET first_name = 'Carly' WHERE ID = 1000;
T
TRUNCATE TABLE - The TRUNCATE statement is used to delete all of
the data in a table. It’s an easy method to remove all data from a table.
You can’t specify a WHERE clause to only delete some data. It’s also
often quicker than a DELETE statement.
TRUNCATE TABLE people;
U
UNION - The UNION keyword allows you to combine the results of two
queries. The data type and number of columns must match.
SELECT columns
FROM table1
UNION
SELECT columns
FROM table2
UNION ALL - The UNION ALL command combines the result set of two
or more SELECT statements (allows duplicate values).
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
UNIQUE - The UNIQUE constraint ensures that all values in a column
are unique.
CREATE TABLE Persons (
ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
UPDATE - The UPDATE command is used to update existing rows in a
table.
UPDATE customer
SET status = 'Inactive'
WHERE customer_id = 4;
V
VALUES - The VALUES command specifies the values of an INSERT INTO
statement.
INSERT INTO customer (customer_id, first_name, last_name)
VALUES (15, 'John', 'Smith');
W
WHERE - The WHERE clause is used in SQL to filter or restrict the rows
that are impacted. It’s used in the SELECT, UPDATE, and DELETE
statements. You specify the conditions that must be true for a row to
be impacted by the statement.
SELECT first_name, last_name
FROM customer
WHERE status = 'Active';