SQL Queries With Example
SQL Queries With Example
Language), DML (Data Manipulation Language), DCL (Data Control Language), and
TCL (Transaction Control Language). We'll use the same sample database for all the
examples. Let's assume we have the following simple database schema for a company
database with two related tables: employees and departments.
Sample Schema:
employees table:
o emp_id (INT)
o emp_name (VARCHAR)
o emp_salary (DECIMAL)
o dept_id (INT, FK referencing departments.dept_id)
departments table:
o dept_id (INT)
o dept_name (VARCHAR)
DDL commands are used to define or modify the structure of a database (e.g., creating,
altering, or deleting tables).
Example:
Creating Tables:
sql
Copy
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(100)
);
sql
Copy
ALTER TABLE employees ADD COLUMN emp_email VARCHAR(100);
Dropping a Table:
sql
Copy
DROP TABLE employees;
DML commands are used to manipulate data within existing tables. These commands are
primarily for querying, inserting, updating, and deleting data.
Example:
Inserting Data:
sql
Copy
INSERT INTO departments (dept_id, dept_name)
VALUES (1, 'HR'), (2, 'Engineering'), (3, 'Sales');
Selecting Data:
sql
Copy
SELECT * FROM employees;
Updating Data:
sql
Copy
UPDATE employees
SET emp_salary = 65000
WHERE emp_id = 103;
Deleting Data:
sql
Copy
DELETE FROM employees WHERE emp_id = 101;
DCL commands are used to control access to data in the database. They deal with
permissions and access control.
Example:
Granting Privileges:
sql
Copy
GRANT SELECT, INSERT ON employees TO user_name;
Revoking Privileges:
sql
Copy
REVOKE SELECT, INSERT ON employees FROM user_name;
TCL commands manage the transactions in the database. They ensure that the database
remains in a consistent state.
Example:
Using Transactions:
sql
Copy
BEGIN;
Using Savepoint:
sql
Copy
BEGIN;
SAVEPOINT savepoint1;
Command
Description Example
Type
Defines or modifies database CREATE TABLE, ALTER TABLE, DROP
DDL
structure TABLE
DML Manipulates data in the database SELECT, INSERT, UPDATE, DELETE
Controls access to data in the
DCL GRANT, REVOKE
database
Manages transactions and ensures COMMIT, ROLLBACK, SAVEPOINT, SET
TCL
consistency TRANSACTION
These commands together help manage a relational database effectively by structuring it,
manipulating data, controlling access, and ensuring transactions are safely handled.
The SQL commands DROP, DELETE, and TRUNCATE are used to remove data or database
objects, but they behave quite differently in terms of scope, usage, and performance. Here's a
breakdown of each command with examples:
1. DROP Command
The DROP command is used to completely remove a table, database, or other database objects
(such as views, indexes, etc.) from the database. When you use DROP, the table structure and
its data are both deleted permanently.
Impact: Removes the table definition and the data. The table is no longer available in
the database, and it cannot be recovered unless you have a backup.
Rollback: You cannot roll back a DROP operation.
Performance: Fast, as it removes the entire table along with its data.
Example:
sql
Copy
-- Drops the entire "employees" table, including its data and structure
DROP TABLE employees;
2. DELETE Command
The DELETE command is used to remove rows from a table based on a condition. It allows
you to specify which rows to delete (through the WHERE clause), and it removes the data but
keeps the structure of the table intact.
Impact: Removes specified rows from the table, but the table itself remains in the
database with its structure.
Rollback: Can be rolled back if used within a transaction (i.e., within a BEGIN and
COMMIT block).
Performance: Slower than TRUNCATE because each row is deleted individually, and it
may also log each row deletion in the transaction log.
Example:
sql
Copy
-- Deletes records where the employee's salary is less than 50000
DELETE FROM employees WHERE emp_salary < 50000;
This will delete specific rows that match the condition in the WHERE clause, but the
table structure will remain intact.
3. TRUNCATE Command
The TRUNCATE command is used to remove all rows from a table, but it does not remove the
table structure. Unlike DELETE, it doesn't allow you to specify conditions for row deletion.
Impact: Removes all rows from the table, but the table structure (including columns,
constraints, etc.) remains. It effectively resets the table, but it does not remove the
table itself.
Rollback: Cannot be rolled back in many databases (it is non-transactional in some
systems). In others, it may be rollbackable within a transaction.
Performance: Faster than DELETE because it doesn't log individual row deletions and
doesn't fire triggers. It simply deallocates the data pages used by the table.
Example:
sql
Copy
-- Removes all rows from the "employees" table, but leaves the structure
intact
TRUNCATE TABLE employees;
This will remove all rows in the employees table, but the table itself remains
available for use.
Use DROP when you want to completely remove a table, including all its data,
constraints, and indexes. This is typically done when you no longer need the table or
want to re-create it from scratch.
Use DELETE when you need to remove specific rows from a table and want to keep the
table structure and constraints intact. It’s useful when you need fine-grained control
over which rows to remove.
Use TRUNCATE when you want to quickly remove all rows from a table without
deleting the structure and you don't need to worry about triggers or the possibility of
rolling back the operation. It’s efficient for clearing out a table when you want to start
fresh but keep the table available for future use.
Example Scenario
Using DROP: If you issue DROP TABLE employees;, the entire employees table
(including data and structure) will be removed from the database. This action is
irreversible.
Using DELETE: If you issue DELETE FROM employees WHERE emp_id = 101;, only
the row for "John Doe" will be deleted, but the employees table will remain with the
other data intact.
Using TRUNCATE: If you issue TRUNCATE TABLE employees;, all rows will be
removed from the employees table, but the table structure will remain, and you can
add more rows later.