SQL
Structured Query Language (SQL), as we all know, is the database language by which we can perform
certain operations on the existing database, and we can also use this language to create a database.
SQL uses certain commands like CREATE, DROP, INSERT, etc. to carry out the required tasks.
SQL Commands are like instructions to a table. It is used to interact with the database with some
operations. It is also used to perform specific tasks, functions, and queries of data. SQL can perform
various tasks like creating a table, adding data to tables, dropping the table, modifying the table, set
permission for users.
SQL Commands are mainly categorized into five categories:
1. DDL – Data Definition Language
2. DQL – Data Query Language
3. DML – Data Manipulation Language
4. DCL – Data Control Language
5. TCL – Transaction Control Language
DDL (Data Definition Language)
DDL or Data Definition Language actually consists of the SQL commands that can be used to
define the database schema. It simply deals with descriptions of the database schema and is
used to create and modify the structure of database objects in the database.
DDL is a set of SQL commands used to create, modify, and delete database structures but not
data. These commands are normally not used by a general user, who should be accessing the
database via an application.
List of DDL Commands:
Here are all the main DDL (Data Definition Language) commands along with their syntax:
Command Description Syntax
Create
database or its
objects (table,
CREATE TABLE table_name
index,
CREATE (column1 data_type,
function,
column2 data_type, ...);
views, store
procedure, and
triggers)
DROP Delete objects DROP TABLE table_name;
from the
Command Description Syntax
database
Alter the ALTER TABLE table_name
ALTER structure of ADD COLUMN column_name
the database data_type;
Remove all
records from a
table, including
TRUNCATE TABLE
TRUNCATE all spaces
table_name;
allocated for
the records are
removed
Add comments
COMMENT 'comment_text'
COMMENT to the data
ON TABLE table_name;
dictionary
Rename an RENAME TABLE
RENAME object existing old_table_name TO
in the database new_table_name;
DQL (Data Query Language)
DQL statements are used for performing queries on the data within schema objects. The
purpose of the DQL Command is to get some schema relation based on the query passed to
it. We can define DQL as follows it is a component of SQL statement that allows getting data
from the database and imposing order upon it. It includes the SELECT statement.
This command allows getting the data out of the database to perform operations with it.
When a SELECT is fired against a table or tables the result is compiled into a further
temporary table, which is displayed or perhaps received by the program i.e. a front-end.
DQL Command
There is only one DQL command in SQL i.e.
Comman
d Description Syntax
SELECT column1,
It is used to retrieve
SELECT column2, ...FROM table_name
data from the database
WHERE condition;
DML (Data Manipulation Language)
The SQL commands that deal with the manipulation of data present in the database belong
to DML or Data Manipulation Language and this includes most of the SQL statements.
It is the component of the SQL statement that controls access to data and to the database.
Basically, DCL statements are grouped with DML statements.
List of DML commands
Here are all the main DML (Data Manipulation Language) commands along with their syntax:
Command Description Syntax
Insert data into INSERT INTO table_name (column1,
INSERT
a table column2, ...) VALUES (value1, value2, ...);
Update existing UPDATE table_name SET column1 =
UPDATE data within a value1, column2 = value2 WHERE
table condition;
Delete records
DELETE FROM table_name WHERE
DELETE from a
condition;
database table
Table control
LOCK LOCK TABLE table_name IN lock_mode;
concurrency
Call a PL/SQL
CALL or JAVA CALL procedure_name(arguments);
subprogram
EXPLAIN Describe the EXPLAIN PLAN FOR SELECT * FROM
PLAN access path to table_name;
Command Description Syntax
data
DCL (Data Control Language)
DCL includes commands such as GRANT and REVOKE which mainly deal with the rights,
permissions, and other controls of the database system.
List of DCL commands:
Two important DCL commands and their syntax are:
Command Description Syntax
Assigns new
privileges to a
GRANT privilege_type
user account,
[(column_list)] ON
allowing access
GRANT [object_type] object_name
to specific
TO user [WITH GRANT
database objects,
OPTION];
actions, or
functions.
Removes
previously
granted privileges REVOKE [GRANT OPTION
from a user FOR] privilege_type
REVOKE account, taking [(column_list)] ON
away their access [object_type] object_name
to certain FROM user [CASCADE];
database objects
or actions.
TCL (Transaction Control Language)
Transactions group a set of tasks into a single execution unit. Each transaction begins with a
specific task and ends when all the tasks in the group are successfully completed. If any of
the tasks fail, the transaction fails.
Therefore, a transaction has only two results: success or failure. You can explore more about
transactions here.
List of TCL Commands
Some TCL commands and their syntax are:
Command Description Syntax
BEGIN Starts a new BEGIN TRANSACTION
TRANSACTION transaction [transaction_name];
Saves all changes
COMMIT made during the COMMIT;
transaction
Undoes all changes
ROLLBACK made during the ROLLBACK;
transaction
Creates a savepoint
SAVEPOINT
SAVEPOINT within the current
savepoint_name;
transaction
Important SQL Commands
Some of the most important SQL commands that you are likely to use frequently include:
1. SELECT: Used to retrieve data from a database.
2. INSERT: Used to add new data to a database.
3. UPDATE: Used to modify existing data in a database.
4. DELETE: Used to remove data from a database.
5. CREATE TABLE: Used to create a new table in a database.
6. ALTER TABLE: Used to modify the structure of an existing table.
7. DROP TABLE: Used to delete an entire table from a database.
8. WHERE: Used to filter rows based on a specified condition.
9. ORDER BY: Used to sort the result set in ascending or descending order.
10. JOIN: Used to combine rows from two or more tables based on a related column between
them.
SQL Commands With Examples
The examples demonstrates how to use an SQL command. Here is the list of popular SQL
commands with Examples.
SQL
Command Example
SELECT SELECT * FROM employees;
INSERT INTO employees (first_name, last_name, email) VALUES
INSERT ('John', 'Doe', '
[email protected]');
UPDATE employees SET email = '[email protected]' WHERE
UPDATE first_name = 'Jane' AND last_name = 'Doe';
DELETE DELETE FROM employees WHERE employee_id = 123;
CREATE CREATE TABLE employees ( employee_id INT PRIMARY KEY,
TABLE first_name VARCHAR(50), last_name VARCHAR(50));
ALTER
ALTER TABLE employees ADD COLUMN phone VARCHAR(20);
TABLE
DROP
DROP TABLE employees;
TABLE
WHERE SELECT * FROM employees WHERE department = 'Sales';
ORDER BY SELECT * FROM employees ORDER BY hire_date DESC;
SELECT e.first_name, e.last_name, d.department_name FROM
employees e JOIN departments d ON e.department_id =
JOIN d.department_id;
Conclusion
SQL commands such as DDL, DML, DCL, DQL, and TCL are foundational for effective database
management. From creating and modifying tables with DDL commands to managing
transactions with TCL commands in SQL, understanding each type of command enhances
your database skills. SQL commands are the foundation of an effective database
management system. Whether you are manipulating data, or managing data, SQL provides
all sets of tools. Now, with this detailed guide, we hope you have gained a deep
understanding of SQL commands, their categories, and syntax with examples.
Give some examples of common SQL commands of each type.
DDL: CREATE, ALTER TABLE, DROP, TRUNCATE, and ADD COLUMN
DML: UPDATE, DELETE, and INSERT
DCL: GRANT and REVOKE
TCL: COMMIT, SET TRANSACTION, ROLLBACK, and SAVEPOINT
DQL: – SELECT
What is a primary key?
A column (or multiple columns) of a table to which the PRIMARY
KEY constraint was imposed to ensure unique and non-null values in
that column. In other words, a primary key is a combination of
the NOT NULL and UNIQUE constraints. The primary key uniquely
identifies each record of the table. Each table should contain a
primary key and can't contain more than one primary key.
19. What is a unique key?
A column (or multiple columns) of a table to which
the UNIQUE constraint was imposed to ensure unique values in that
column, including a possible NULL value (the only one).
20. What is a foreign key?
A column (or multiple columns) of a table to which the FOREIGN
KEY constraint was imposed to link this column to the primary key in
another table (or several tables). The purpose of foreign keys is to
keep connected various tables of a database.
21. What is an index?
A special data structure related to a database table and used for
storing its important parts and enabling faster data search and
retrieval. Indexes are especially efficient for large databases, where
they significantly enhance query performance.
What types of SQL operators do you know?
Arithmetic (+, -, *, /, etc.)
Comparison (>, <, =, >=, etc.)
Compound (+=, -=, *=, /=, etc.)
Logical (AND, OR, NOT, BETWEEN, etc.)
String (%, _, +, ^, etc.)
Set (UNION, UNION ALL, INTERSECT, and MINUS (or EXCEPT))
How to select all even or all odd records in a table?
By checking the remainder of the division by 2. In some SQL versions
(e.g., PostgreSQL and My SQL), we use the MOD function, in the others
(Microsoft SQL Server and SQLite) – the modulo operator ( %). To select all
even records using MOD:
SELECT * FROM table_name
WHERE MOD(ID_column, 2) = 0;
SELECT * FROM table_name
WHERE ID_column % 2 = 0;
How to find the last id in a table?
Using the MAX() function. Otherwise, in many SQL versions, we can use the
following syntax:
SELECT id
FROM table_name
ORDER BY id DESC
LIMIT 1;
or in Microsoft SQL Server:
SELECT TOP 1 id
FROM table_name
ORDER BY id DESC