SQL (Structured Query Language) is a standard language for managing and manipulating relational databases. Understanding SQL syntax is crucial for querying, updating, and managing data stored in databases. This chapter covers the fundamental aspects of SQL syntax, from basic to advanced topics.
SQL is a language used to communicate with databases. It is the standard language for relational database management systems. SQL statements are used to perform tasks such as updating data in a database or retrieving data from a database.
SQL statements are the instructions that tell the database what actions to perform. They can be broadly categorized into the following types:
Defines the structure of the database.
CREATE
: Creates objects in the database.ALTER
: Alters the structure of the database.DROP
: Deletes objects from the database.Manages data within schema objects.
SELECT
: Retrieves data from the database.INSERT
: Inserts data into a table.UPDATE
: Updates existing data within a table.DELETE
: Deletes records from a table.Controls access to data in the database.
GRANT
: Gives a privilege to a user.REVOKE
: Takes back privileges granted to a user.Manages changes made by DML statements.
COMMIT
: Saves the work done.ROLLBACK
: Restores the database to the last committed state.SAVEPOINT
: Sets a point within a transaction to which a rollback can be performed.The general structure of an SQL statement is as follows:
;
SELECT * FROM employees;
SELECT
: Keyword to retrieve data.*
: Indicates all columns.FROM
: Keyword to specify the table.employees
: The name of the table.employee_id | first_name | last_name | department |
---|---|---|---|
1 | John | Doe | HR |
2 | Jane | Smith | Finance |
CREATE DATABASE mydatabase;
CREATE DATABASE
: Command to create a new database.mydatabase
: The name of the new database.
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50)
);
CREATE TABLE
: Command to create a new table.employees
: The name of the table.employee_id
, first_name
, last_name
, and department
.
ALTER TABLE employees ADD COLUMN salary DECIMAL(10, 2);
ALTER TABLE
: Command to modify an existing table.ADD COLUMN
: Adds a new column to the table.salary DECIMAL(10, 2)
: Specifies the new column name and its data type.
DROP TABLE employees;
DROP TABLE
: Command to delete a table.employees
: The name of the table to be deleted.
INSERT INTO employees (employee_id, first_name, last_name, department)
VALUES (1, 'John', 'Doe', 'HR');
INSERT INTO
: Command to insert new data into a table.employees
: The name of the table.employee_id
, first_name
, last_name
, department
.VALUES
: Keyword followed by the values to insert.
UPDATE employees
SET department = 'Finance'
WHERE employee_id = 1;
UPDATE
: Command to update existing data.employees
: The name of the table.SET
: Keyword to specify the new values.department = 'Finance'
: The new value for the department column.WHERE
: Clause to specify which rows to update.
DELETE FROM employees
WHERE employee_id = 1;
DELETE FROM
: Command to delete data from a table.employees
: The name of the table.WHERE
: Clause to specify which rows to delete.
SELECT * FROM employees;
SELECT
: Command to retrieve data.*
: Indicates all columns.FROM
: Keyword to specify the table.employees
: The name of the table.Joins are used to combine rows from two or more tables based on a related column.
SELECT employees.first_name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;
INNER JOIN
: Combines rows from both tables where the condition is met.ON
: Clause to specify the join condition.first_name | department_name |
---|---|
John | HR |
Jane | Finance |
SELECT employees.first_name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.department_id;
LEFT JOIN
: Combines rows from the left table with matching rows from the right table and returns all rows from the left table.
first_name | department_name |
---|---|
John | HR |
Jane | Finance |
Mike | NULL |
Subqueries are nested queries within a larger query.
SELECT first_name
FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE department_name = 'HR');
SELECT department_id FROM departments WHERE department_name = 'HR'
SELECT first_name FROM employees WHERE department_id = (subquery)
first_name |
---|
John |
Aggregate functions perform calculations on a set of values and return a single value.
SELECT COUNT(*) FROM employees;
COUNT(*)
: Returns the number of rows in the table.
count |
---|
3 |
SELECT SUM(salary) FROM employees;
SUM(salary)
: Returns the total sum of the salary column.
sum |
---|
150000 |
SELECT AVG(salary) FROM employees;
AVG(salary)
: Returns the average value of the salary column.
avg |
---|
50000 |
SELECT MAX(salary) FROM employees;
MAX(salary)
: Returns the maximum value in the salary column.
max |
---|
70000 |
SELECT MIN(salary) FROM employees;
MIN(salary)
: Returns the minimum value in the salary column.
min |
---|
30000 |
GRANT SELECT ON employees TO user1;
GRANT
: Command to give privileges.SELECT ON employees
: Specifies the privilege and the table.TO user1
: Specifies the user to grant the privilege to.
REVOKE SELECT ON employees FROM user1;
REVOKE
: Command to take back privileges.SELECT ON employees
: Specifies the privilege and the table.FROM user1
: Specifies the user to revoke the privilege from.
BEGIN;
INSERT INTO employees (employee_id, first_name, last_name, department) VALUES (3, 'Mike', 'Johnson', 'IT');
COMMIT;
BEGIN
: Starts a new transaction.INSERT
: Adds a new row to the employees table.COMMIT
: Saves the transaction.
BEGIN;
DELETE FROM employees WHERE employee_id = 3;
ROLLBACK;
BEGIN
: Starts a new transaction.DELETE
: Deletes a row from the employees table.ROLLBACK
: Reverts the transaction.
BEGIN;
INSERT INTO employees (employee_id, first_name, last_name, department) VALUES (4, 'Anna', 'Williams', 'IT');
SAVEPOINT sp1;
UPDATE employees SET department = 'HR' WHERE employee_id = 4;
ROLLBACK TO sp1;
COMMIT;
SAVEPOINT sp1
: Creates a savepoint within the transaction.ROLLBACK TO sp1
: Rolls back to the savepoint.COMMIT
: Saves the transaction.This chapter covered the basics of SQL syntax, from defining databases and tables to manipulating data and managing transactions. Understanding these fundamental concepts is essential for effectively working with relational databases. By mastering SQL syntax, you can efficiently query, update, and manage your data. Happy coding !❤️