SQL Commands List For The Most Common Actions: 7 Min Jun 3, 2019 Oct 3, 2019
SQL Commands List For The Most Common Actions: 7 Min Jun 3, 2019 Oct 3, 2019
PublishedJun 3, 2019
UpdatedOct 3, 2019
This is a SQL commands list that covers all the necessary actions with SQL
databases. Each SQL command is provided with its definition, a code
snippet that represents the correct syntax, and some have live code
examples that you can try modifying to see the command in action.
Contents
1. SQL Commands List
1.1. AND|OR
1.2. ALTER TABLE
1.3. AS (alias)
1.4. BETWEEN
1.5. CREATE DATABASE
1.6. CREATE TABLE
1.7. CREATE INDEX
1.8. CREATE VIEW
1.9. DELETE
1.10. GRANT
1.11. REVOKE
1.12. COMMIT
1.13. ROLLBACK
1.14. SAVEPOINT
1.15. DROP DATABASE
1.16. DROP INDEX
1.17. DROP TABLE
1.18. EXISTS
1.19. GROUP BY
1.20. HAVING
1.21. IN
1.22. INSERT INTO
1.23. INNER JOIN
1.24. LEFT JOIN
1.25. RIGHT JOIN
1.26. FULL JOIN
1.27. LIKE
1.28. ORDER BY
1.29. SELECT
1.30. SELECT *
1.31. SELECT DISTINCT
1.32. SELECT INTO
1.33. SELECT TOP
1.34. TRUNCATE TABLE
1.35. UNION
1.36. UNION ALL
1.37. UPDATE
1.38. WHERE
AND|OR
AND combines two or more conditions for a single query. All of the
conditions used with this operator must be met in order to show the
results.
Example Copy
SELECT * FROM Developers
WHERE Country='India' AND City='Delhi';
Try it Live
OR is used similarly, but it will output result with rows that comply
with either of the conditions.
Example Copy
SELECT * FROM Developers
WHERE City='London' OR City='Paris';
Try it Live
ALTER TABLE
ALTER TABLE allows you to add or remove columns from a table.
Example Copy
ALTER TABLE Developers
ADD BirthDate date;
Example Copy
ALTER TABLE Developers
DROP COLUMN BirthDate;
AS (alias)
AS allows you to rename a column or table to a more convenient alias (a
correlation name) without changing the original names in the database.
This makes writing queries easier when the original table or column
names are long or complicated.
Example Copy
SELECT ID as CustomerID, Name AS Customers
FROM Customers;
Try it Live
Example Copy
SELECT o.ID, c.Name
FROM Customers AS c, Customer_orders AS o
WHERE c.id = 2 AND c.ID = o.customer_id;
Try it Live
BETWEEN
BETWEEN operator filters the results and returns only the ones that fit the
specified range. You can describe the value of this operator using dates,
numbers, or text.
Example Copy
SELECT * FROM Orders
WHERE Price BETWEEN 10 AND 15;
Try it Live
CREATE DATABASE
When you need to create a new database, use the CREATE DATABASE
statement. You must have admin rights to do that.
Example Copy
CREATE DATABASE testingDB;
CREATE TABLE
CREATE TABLE statement creates a new table in a database.
Example Copy
CREATE TABLE Suppliers (
SupplierID int,
FirstName varchar(255),
LastName varchar(255),
City varchar(255),
Country varchar(255)
);
CREATE INDEX
CREATE INDEX generates an index for a table. This enables retrieving data
from a database faster. Users don't see indexes as they are only used to
increase search speed.
Example Copy
CREATE INDEX idx_lastname
ON Persons (LastName);
CREATE VIEW
CREATE VIEW creates a narrower version of an existing table by getting a
set of results based on a certain query. A view is not much different from
a real table: it contains columns and rows with data, but it doesn't contain
the fields of the real table that are irrelevant for your particular purpose.
Example Copy
CREATE VIEW [Present List Products] AS
SELECT ID, Name
FROM Products
WHERE Discontinued = No;
DELETE
If you need to remove certain rows from the table, use the DELETE FROM
statement.
Example Copy
DELETE FROM Developers
WHERE Name='Antonio Indigo';
Example Copy
DELETE * FROM Developers;
GRANT
GRANT command is for giving users the access to a database.
Example Copy
GRANT SELECT, UPDATE ON YOUR_TABLE TO FIRST_USER, SECOND_USER;
REVOKE
REVOKE command is for taking away users' permisions.
Example Copy
REVOKE SELECT, UPDATE ON YOUR_TABLE FROM FIRST_USER, SECOND_USER;
COMMIT
COMMIT command is for saving every transaction to the database.
Example Copy
DELETE FROM CUSTOMERS
WHERE AGE = 18;
COMMIT;
ROLLBACK
ROLLBACK command is for undoing transactions which are not saved to
the database.
Example Copy
DELETE FROM CUSTOMERS
WHERE AGE = 18;
ROLLBACK;
SAVEPOINT
SAVEPOINT command is for returning a transaction to a specific point
without affecting the whole transaction.
Example Copy
SAVEPOINT SAVEPOINT_NAME;
DROP DATABASE
DROP DATABASE is one of the riskiest statements that should be used
with extra caution. In SQL, drop means delete – and DROP DATABASE
deletes the whole specified database together with all its parameters and
data.
Example Copy
DROP DATABASE db_name
DROP INDEX
DROP INDEX will delete the index you specify. The syntax of this statement
varies based on the DB system used.
SQL Server
Example Copy
DROP INDEX tbl_name.indx_name
MS Access
Example Copy
DROP INDEX indx_name ON tbl_name
DB2/Oracle
Example Copy
DROP INDEX indx_name
MySQL
Example Copy
ALTER TABLE tbl_name DROP INDEX indx_name
DROP TABLE
DROP TABLE statement deletes the whole table with its column
parameters and datatype settings. If you want to remove only the
contents of the rows but keep the table itself, use another statement –
TRUNCATE TABLE.
Example Copy
DROP TABLE tbl_name
EXISTS
EXISTS operator allows you to check whether a record exists by writing a
subquery. If the record is found, the result is displayed based on the
statement you use this operator with. You can use it with SELECT, UPDATE,
INSERT, and DELETE.
Example Copy
SELECT id, name
FROM customers
WHERE EXISTS (SELECT id FROM customer_orders WHERE customer_orders.customer_id =
customers.id AND customers.city = "Rome");
Try it Live
GROUP BY
Combine GROUP BY with SELECT statement in order to arrange identical
data (rows with the same value) into groups (summarizing rows).
Example Copy
SELECT COUNT(ID), City
FROM Developers
GROUP BY City;
Try it Live
HAVING
HAVING specifies that you need to filter the results to only the rows that
fulfill the described condition.
It performs the same action as the WHERE clause. The difference is that
HAVING is used only for aggregate functions as WHERE doesn't work with
them.
Example Copy
SELECT COUNT(ID), Country
FROM Pets
GROUP BY Country
HAVING COUNT(ID) > 2;
Try it Live
IN
The IN operator includes multiple values into the WHERE clause.
Example Copy
SELECT * FROM Developers
WHERE Country IN ('USA', 'France', 'India');
Try it Live
INSERT INTO
INSERT INTO statement inserts new rows of data into a table.
Example Copy
INSERT INTO Developers (Name, City, Country)
VALUES ('Luke Christon', 'London', 'UK');
INNER JOIN
INNER JOIN combines rows from different tables.
Example Copy
SELECT Orders.ID, Developers.Name
FROM Orders
INNER JOIN Developers ON Orders.ID = Developers.ID;
Try it Live
LEFT JOIN
LEFT JOIN retrieves records from the left table that match records in the
right table. Some databases have a slightly different statement for this –
LEFT OUTER JOIN.
Example Copy
SELECT Developers.Name, Customer_orders.ID
FROM Developers
LEFT JOIN Customer_orders ON Developers.ID = Customer_orders.customer_id
ORDER BY Developers.Name;
Try it Live
RIGHT JOIN
RIGHT JOIN retrieves records from the right table that match records in
the left table. Some databases call this statement differently – RIGHT
OUTER JOIN.
Example Copy
SELECT Customer_orders.ID, Employees.Last_name, Employees.First_name
FROM Customer_orders
RIGHT JOIN Employees ON Customer_orders.employee_id = Employees.ID
ORDER BY Customer_orders.ID;
FULL JOIN
FULL JOIN returns all the records that match either in left or right tables.
Example Copy
SELECT Customers.Name, Customer_orders.ID
FROM Customers
FULL OUTER JOIN Orders ON Customers.ID=Customer_orders.customer_id
ORDER BY Customers.Name;
LIKE
Combine LIKE with the WHERE clause for finding specific patterns in
columns.
Example Copy
SELECT * FROM users WHERE email LIKE '%gmail%';
Try it Live
ORDER BY
ORDER BY sets the order (ascending by default) of result records.
Example Copy
SELECT * FROM users ORDER BY email DESC;
Try it Live
SELECT
SELECT is one of the main SQL statements. It selects data from a database
and returns the table of results, called the result-set.
Example Copy
SELECT username,email
FROM users;
Try it Live
SELECT *
SELECT used with an asterisk * operator selects all data records from a
specified table.
Example Copy
SELECT * FROM Customers;
Try it Live
SELECT DISTINCT
SELECT DISTINCT returns only the data that is distinct, and does not
include duplicate entries.
Example Copy
SELECT DISTINCT City FROM Developers;
Try it Live
SELECT INTO
SELECT INTO statement selects specified data in a table and copies it to
another table.
Example Copy
SELECT * INTO CustomerBackup2018
FROM Customers;
Example Copy
SELECT Name, Contact INTO CustomerBackup2017
FROM Customers;
SELECT TOP
SELECT TOP specifies the maximum number or percentage of data entries
to return in a result-set.
Example Copy
SELECT * FROM Customers
LIMIT 3;
Try it Live
Example Copy
SELECT TOP 50 PERCENT * FROM Customers;
TRUNCATE TABLE
TRUNCATE TABLE removes data entries from a table in a database, but
keeps the table, its datatype and column parameters.
Example Copy
TRUNCATE TABLE tbl_name
UNION
You can combine multiple result-sets using the UNION operator with two
or more SELECT statements.
Example Copy
SELECT City FROM Developers
UNION
SELECT City FROM Customers
ORDER BY City;
Try it Live
UNION ALL
UNION ALL is used to combine two or more result-sets and keep all the
duplicate data entries.
Example Copy
SELECT City FROM Developers
UNION ALL
SELECT City FROM Customers
ORDER BY City;
Try it Live
UPDATE
UPDATE statement is used with the WHERE clause to update data in the
table.
Example Copy
UPDATE Developers
SET City = 'Berlin', Country= 'Germany'
WHERE Name = 'Antonio Indigo';
WHERE
WHERE clause specifies your query to filter only the results that satisfy
your set condition.
WHERE doesn't work with the aggregate functions, for that purpose, use
HAVING instead.
Example Copy
SELECT * FROM Developers
WHERE Country='France';