Structured Query Language
Structured Query Language
Structured
Query
Language
• SQL is a computer language for managing and retrieving data in relational databases.
• It is the standard language for relational database management systems (RDBMS).
• Popular RDBMS like MySQL, MS Access, Oracle, and SQL Server use SQL.
• Each RDBMS may have its own dialect of SQL, such as T-SQL for MS SQL Server
and PL/SQL for Oracle.
• SQL allows for storing, manipulating, and retrieving data in a structured manner.
• SQL supports operations like data insertion, modification, deletion, and querying.
• It provides a standardized way to define database structures, create tables, and
establish relationships.
• SQL queries are used to retrieve specific data from databases using SELECT
Example: Suppose we have a table named "customers" that contains customer records, and we want
to delete all the data from this table:
Example: Suppose we have a table called "customers" that we want to remove from the database:
This command will delete the "customers" table from the database, along with all its data and related objects. It is
important to note that once the table is dropped, it cannot be recovered, so it is advisable to take backups or ensure
the table removal is intentional.
INSERT Query
The INSERT command is used to insert new rows of data into a specified table in a database. It allows
you to specify the columns you want to insert data into and the corresponding values for those columns.
Syntax
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Example: Suppose we have a table called "employees" with columns "id", "name", and "salary". We
want to insert a new employee into the table:
INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 5000);
SELECT Query
The SELECT command is used to retrieve data from one or more tables in a database. It allows you to
specify the columns you want to retrieve and optionally apply conditions to filter the data.
Syntax
Example: Suppose we have a table called "customers" with columns "id", "name", and "city". We want to
retrieve the names of all customers from the city 'New York':
The WHERE clause is used in conjunction with the SELECT statement to filter data based on specific
conditions. It allows you to specify a condition that must be met for a row to be included in the result set
Syntax
The AND and OR operators are used in conjunction with the WHERE
clause to create more complex conditions for filtering data.
•AND: The AND operator is used to combine multiple conditions, and all
the conditions must evaluate to true for a row to be included in the result
set.
•OR: The OR operator is used to combine multiple conditions, and at
least one of the conditions must evaluate to true for a row to be included
in the result set.
Syntax with AND
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2;
Example with AND: Suppose we have a table called "employees" with columns "id",
"name", "age", and "department". We want to retrieve all employees who are both
from the "Sales" department and are above 30 years old:
SELECT * FROM employees
WHERE department = 'Sales' AND age > 30;
Syntax with OR
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2;
Example with OR: Suppose we have a table called "products" with columns "id",
"name", and "category". We want to retrieve all products that are either in the
"Electronics" category or have a price greater than 1000:
SELECT * FROM products
WHERE category = 'Electronics' OR price > 1000;
UPDATE Query
The UPDATE command is used to modify existing data in a table. It allows you to update specific columns
with new values based on specified conditions.
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example: Suppose we have a table called "employees" with columns "id", "name", and "salary". We want to
update the salary of an employee with ID 1 to 6000:
UPDATE employees
SET salary = 6000
WHERE id = 1;
DELETE Query
The DELETE command is used to remove rows from a table based on specified conditions. It allows you to
selectively delete data from a table.
Syntax
DELETE FROM table_name
WHERE condition;
Example: Suppose we have a table called "customers" with columns "id", "name", and "city". We want to
delete all customers from the city 'London':
Example:
Example:
SELECT name, age
FROM customers
WHERE country = 'USA'
LIMIT 5;
3."ROWNUM" (Oracle):
SELECT column1, column2, ...
FROM table_name
WHERE ROWNUM <= number_of_rows;
ORDER BY Clause
The ORDER BY clause is used to sort the result of a query based on one or more columns. It allows you to specify
the sorting order, either in ascending (ASC) or descending (DESC) order.
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
Example: Suppose we have a table called "employees" with columns "id", "name", and "salary". We want to retrieve
all employees' names and salaries in descending order of their salaries:
Example: Suppose we have a table called "employees" with columns "id", "name", and "department".
We want to retrieve all unique department names from the table.
Example: Suppose we have a table called "products" with columns "product_id", "name", and "price". We
want to retrieve all products sorted by their price in ascending order.
Example:We want to retrieve a combined list of unique customer IDs from both the "orders" and
"invoices" tables. We can use the UNION operator to achieve this:
SELECT customer_id
FROM orders
UNION
SELECT customer_id FROM invoices;
Intersect
In SQL, the INTERSECT clause is used to combine the result sets of two or more SELECT statements and
return only the rows that are common to all the result sets. It returns the intersection of rows between the result
sets, meaning only the rows that exist in all SELECT statements will be included in the final result set.
Syntax:
SELECT column1, column2, ...
FROM table1
INTERSECT
SELECT column1, column2, ...
FROM table2;
Example:Suppose we have two tables, "employees" and "managers", both with a column "employee_name". We want
to find the names of employees who are also managers in the company.
SELECT employee_name
FROM employees
INTERSECT
SELECT employee_name FROM managers;
Except
The EXCEPT operator in SQL is used to subtract the result set of one SELECT statement from the result set of
another SELECT statement. It returns only the rows that exist in the first SELECT statement but not in the
second SELECT statement
Syntax:
SELECT column1, column2, ...
FROM table1
EXCEPT
SELECT column1, column2, ...
FROM table2;
Suppose we have two tables, "employees" and "managers", both with a column "employee_name". We want to
find the names of employees who are not managers.
SELECT employee_name
FROM employees
EXCEPT
SELECT employee_name FROM managers;
Alias Syntax
In SQL, an alias is used to assign a temporary name or alias to a table or column in a query. Aliases can make the query
more readable and provide a shorthand notation for referring to tables or columns.
Here's the syntax for assigning aliases in SQL:
Table Alias:
SELECT column1, column2, ...
FROM table_name AS alias_name;
Column Alias:
SELECT column_name AS alias_name
FROM table_name;
Example
SELECT e.employee_name AS name, d.department_name
FROM employees AS e
JOIN departments AS d ON e.department_id =
d.department_id;
Like Operator
In SQL, the LIKE operator is used in a WHERE clause to search for a specified pattern in a column. It is
commonly used with string values to perform pattern matching.
The LIKE operator uses two wildcard characters:
•'%' (percent sign): Represents any sequence of characters (including none).
•'_' (underscore): Represents any single character.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;
Example : Suppose we have a table named "products" with a column called
"product_name". We want to retrieve all products that start with the letter 'A'.
sql
SELECT product_name
FROM products
WHERE product_name LIKE 'A%';
SQL CONSTRAINTS
In SQL, constraints are used to enforce rules or conditions on the columns or tables
to maintain the integrity, consistency, and validity of the data. There are several
types of constraints commonly used in SQL:
Primary Key Constraint:
•Ensures that a column or a combination of columns uniquely identifies
each row in a table.
•Example:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
...
);
Foreign Key Constraint:
•Establishes a relationship between two tables based on a column(s) from one
table referencing the primary key column(s) of another table.
•Example: