SQL Commands
SQL Commands
Data Definition Language (DDL) commands are used for defining the
database structure or schema.
Let's look at some DDL commands with a simple example for each
command.
i. CREATE
In SQL, the CREATE TABLE command is used to create a new table in the
database. For example,
product_id INT,
name VARCHAR(100),
price DECIMAL
);
Run Code
Here, the SQL command creates a new table named Products with three
columns: product_id (integer type), name (string type up
to 100 characters), and price (decimal type for storing prices).
Run Code
In SQL, the DROP TABLE command is used to delete the specified table in
our database. For example,
Run Code
Here, the SQL command will delete the table named Orders.
Let's look at some DML commands with a simple example for each
command.
i. INSERT INTO
In SQL, the INSERT INTO statement is used to insert new rows into a
database table. For example,
Run Code
Here, the SQL command inserts a new row into the Customers table with
the given values.
ii. UPDATE
UPDATE Customers
SET age = 29
WHERE customer_id = 5;
Run Code
iii. DELETE
The SQL DELETE statement is used to delete row(s) from a database table.
For example,
Run Code
Here, the SQL command deletes all rows from the Customers table where
the country is UAE.
DQL is used for querying and retrieving data from a database. It allows us
to specify the exact data we want to see from one or more tables based
on given conditions.
SELECT
Run Code
Here, the SQL command retrieves all rows from the Customers table
where the country is UK.
DCL commands include GRANT and REVOKE, which are used to control
access to the database.
i. GRANT
ii. REVOKE
In SQL, the REVOKE statement withdraws access privileges given by
the GRANT statement.
Here, the SQL query revokes SELECT permission on the Customers table
from user1.
i. COMMIT
In SQL, the COMMIT command is used for saving the changes made in the
database. For example,
UPDATE Customers
WHERE customer_id = 4;
COMMIT;
ii. ROLLBACK
In SQL, ROLLBACK is used to undo the transactions that have not been
saved in the database. For example,
ROLLBACK;
Here, the query deletes all records from the Orders table but then rolls
back the transaction.
SQL Commands | DDL, DQL, DML, DCL and TCL Commands
column_name data_type;
COMMENT ON TABLE
Add comments to the
COMMENT table_name IS
data dictionary
'comment_text';
RENAME TABLE
Rename an object
RENAME old_table_name TO
existing in the database
new_table_name;
Example:
In this example, a new table called employees is created with columns for
employee ID, first name, last name and hire date.
DQL is used to fetch data from the database. The main command is
SELECT, which retrieves records based on the query. The output is
returned as a result set (a temporary table) that can be viewed or used in
applications.
Comman
d Description Syntax
SELECT column1
Filters rows before any
WHERE FROM table_name
grouping or aggregation
WHERE condition;
SELECT column1,
Groups rows that have the
GROUP AVG_FUNCTION(column2)
same values in specified
BY FROM table_name
columns.
GROUP BY column1;
SELECT column1,
AVG_FUNCTION(column2)
Filters the results of GROUP
HAVING FROM table_name
BY
GROUP BY column1
HAVING condition;
SELECT column1
ORDER Sorts the result set by one FROM table_name
BY or more columns ORDER BY column1 [ASC |
DESC];
Note: DQL has only one command, SELECT. Other terms like FROM,
WHERE, GROUP BY, HAVING, ORDER BY, DISTINCT and LIMIT are clauses of
SELECT, not separate commands.
Example:
This query retrieves employees first and last names, along with their hire
dates, from the employees table, specifically for those in the 'Sales'
department, sorted by hire date.
Comma
nd Description Syntax
Example:
INSERT INTO employees (first_name, last_name, department)
VALUES ('Jane', 'Smith', 'HR');
This query inserts a new record into employees table with first name
'Jane', last name 'Smith' and department 'HR'.
Example:
This command grants the user user_name the permissions to select and
update records in the employees table.
Creates a savepoint
SAVEPOINT
SAVEPOINT within the current
savepoint_name;
transaction
Example:
BEGIN TRANSACTION;
UPDATE employees SET department = 'Marketing' WHERE department =
'Sales';
SAVEPOINT before_update;
UPDATE employees SET department = 'IT' WHERE department = 'HR';
ROLLBACK TO SAVEPOINT before_update;
COMMIT;