Nots RDBMS
Nots RDBMS
)
1.1Characteristics of database approach
Data Independence:
Logical Independence: Applications are insulated from changes in the logical
structure of the data.
Physical Independence: Applications are insulated from changes in the physical
storage structure of the data.
Data Integration:
Integration of data from multiple sources into a single, unified database, which
provides a consistent view of data.
Data Integrity:
Ensuring the accuracy, consistency, and validity of data stored in the database.
Data Security:
Implementation of measures to protect the database from unauthorized access, data
corruption, and loss.
Concurrent Access and Transaction Control:
Managing simultaneous access to the database by multiple users or applications
without interference.
Ensuring the ACID properties (Atomicity, Consistency, Isolation, Durability) for
transactions.
Data Modeling:
Using conceptual, logical, and physical data models to design the database structure
that fits the requirements of the organization.
Query Language:
Providing a standardized language (e.g., SQL) for users and applications to retrieve
and manipulate data stored in the database.
Backup and Recovery:
Establishing procedures and mechanisms for backing up data regularly and recovering
data in case of hardware failures, human errors, or other disasters.
Scalability and Performance:
Designing the database system to efficiently handle increasing amounts of data and
user requests while maintaining acceptable performance levels.
Data Warehousing and Analytics:
Supporting the extraction, transformation, and loading (ETL) of data into data
warehouses for business intelligence and decision-making purposes.
Database Administration:
Responsibilities of database administrators (DBAs) in managing and maintaining
databases, ensuring availability, security, and performance.
Normalization:
Applying normalization techniques to reduce redundancy and improve data integrity
in relational database designs.
SET TaughtBy
Owner: Instructor
Members: Course
3. Relational Data Model
The relational data model organizes data into tables (relations), where each table consists of
rows (tuples) and columns (attributes). Tables can be related to each other using foreign keys.
This model is highly flexible and widely used due to its simplicity and robustness.
Example: E-commerce Application
Entities and Attributes:
Customer: CustomerID, Name, Email
Order: OrderID, OrderDate, CustomerID
Product: ProductID, ProductName, Price
OrderItem: OrderItemID, OrderID, ProductID, Quantity
Relationships:
A customer can place multiple orders.
An order can have multiple order items.
An order item is associated with one product.
Tables:
CREATE TABLE Customer (
CustomerID INT PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100)
);
-- Members table
INSERT INTO Members (MemberID, Name, Address, Email) VALUES
(1, 'John Doe', '123 Elm St', '[email protected]'),
(2, 'Jane Smith', '456 Oak St', '[email protected]'),
(3, 'Alice Johnson', '789 Pine St', '[email protected]');
-- Loans table
INSERT INTO Loans (LoanID, BookID, MemberID, LoanDate, ReturnDate) VALUES
(1, 1, 1, '2024-07-01', '2024-07-15'),
(2, 2, 2, '2024-07-03', '2024-07-17'),
(3, 3, 3, '2024-07-05', '2024-07-19');
In this example:
The schema defines the structure of the Books, Members, and Loans tables.
The instance includes the actual records inserted into these tables at a specific time.
1.8 Terminologies: relation, attribute, domain, tuple, entities, Integrity Constraints (Domain, Entity,
Referential)
1. Relation:
Definition: In the context of relational databases, a relation refers to a table that holds
data organized in rows (tuples) and columns (attributes).
Usage: Each row represents a record (tuple) and each column represents an attribute
(field or property). Relations are fundamental to the relational model and are
commonly referred to as tables in database management systems.
2. Attribute:
Definition: An attribute is a named column of a relation (table) that represents a
specific characteristic or property of the entity (record or tuple).
Usage: Attributes correspond to fields in a record and define the types of data that can
be stored in each column. For example, in a "Student" relation, attributes could
include "StudentID," "Name," "Age," and "GPA."
3. Domain:
Definition: Domain refers to the set of allowable values for an attribute.
Usage: Each attribute in a relation is associated with a domain that specifies the type
of data it can hold (e.g., integer, string, date) and any constraints on the values (e.g.,
range limits, format).
4. Tuple:
Definition: A tuple is a single record or row in a relation (table).
Usage: Tuples represent individual instances or entities within the domain described
by the relation's schema. For example, in a "Customer" relation, each tuple would
represent a specific customer with attributes like "CustomerID," "Name," "Address,"
etc.
5. Entities:
Definition: In the context of databases, entities are objects or things that are
represented in the database and have an independent existence.
Usage: Entities are typically represented as tables in a relational database, where each
entity corresponds to a relation. For example, a "Product" entity would be represented
as a "Product" relation with attributes like "ProductID," "Name," "Price," etc.
6. Integrity Constraints:
Integrity constraints are rules or conditions that enforce the validity, accuracy, and
consistency of data in a relational database. There are several types of integrity constraints:
Domain Constraints:
o Definition: Domain constraints define the allowable values for an attribute
based on its domain.
o Usage: Example constraints include data type (e.g., integer, string) and
constraints such as range limits or format (e.g., date format).
Entity Integrity Constraints:
o Definition: Entity integrity constraints ensure that each tuple in a relation has
a unique and non-null primary key value.
o Usage: This constraint ensures that primary key attributes cannot have null
values and must be unique within the relation.
Referential Integrity Constraints:
o Definition: Referential integrity constraints ensure the consistency between
related tables by maintaining referential relationships between tables.
o Usage: These constraints typically involve foreign keys that reference primary
keys in other tables. They ensure that a foreign key value in one table matches
an existing primary key value in another table or is null.
1.11 Key: Super key, Composite Key, Candidate Key, Primary Key, Alternate Key or Secondary Key,
Foreign Key),
In database design, keys are crucial for identifying records uniquely within a table. Here are
the definitions and examples of different types of keys:
1. Super Key
A super key is any combination of columns that uniquely identifies a row in a table. It can
include additional columns beyond the minimum required to ensure uniqueness.
Example:
In a table Students with columns (StudentID, Name, Email), possible super keys
are:
o (StudentID)
o (Email)
o (StudentID, Email)
o (StudentID, Name, Email)
2. Composite Key
A composite key is a key that consists of two or more columns to uniquely identify a row in a
table. This is necessary when a single column is not sufficient to ensure uniqueness.
Example:
In a table Enrollments with columns (StudentID, CourseID, EnrollmentDate),
a composite key could be:
o (StudentID, CourseID)
3. Candidate Key
A candidate key is a minimal super key; it is a super key without any redundant attributes. It
uniquely identifies a row in a table and there can be multiple candidate keys in a table.
Example:
In a table Students with columns (StudentID, Name, Email), possible candidate
keys are:
o (StudentID)
o (Email)
4. Primary Key
A primary key is a candidate key chosen by the database designer to uniquely identify rows
in a table. It must contain unique values and cannot contain null values.
Example:
In a table Students with columns (StudentID, Name, Email), if StudentID is
chosen as the primary key, it ensures each student is uniquely identifiable.
5. Alternate Key (or Secondary Key)
An alternate key is any candidate key that is not chosen as the primary key. It is still capable
of uniquely identifying rows in the table.
Example:
Continuing with the Students table example, if StudentID is chosen as the primary
key, then Email would be an alternate key.
Example Table: Students
StudentID Name Email
1 Alice [email protected]
2 Bob [email protected]
3 Charlie [email protected]
Super Key: (StudentID), (Email), (StudentID, Email), (StudentID, Name, Email)
Composite Key: Not applicable here since single columns are sufficient.
Candidate Key: (StudentID), (Email)
Primary Key: (StudentID)
Alternate Key: (Email)
Example Table: Enrollments
StudentID CourseID EnrollmentDate
1 101 2024-01-15
2 102 2024-01-16
1 102 2024-01-17
Super Key: (StudentID, CourseID), (StudentID, CourseID, EnrollmentDate)
Composite Key: (StudentID, CourseID)
Candidate Key: (StudentID, CourseID)
Primary Key: (StudentID, CourseID)
Alternate Key: None in this case as there is only one candidate key.
Characteristics of Foreign Keys:
1. Referential Integrity: Foreign keys help maintain referential integrity by ensuring that a
value in one table corresponds to an existing value in another table.
2. Relationships: They establish relationships between tables, such as one-to-one, one-to-
many, or many-to-many.
3. Cascading Actions: In some database systems, foreign keys can enforce cascading actions
like updates and deletions. For example, if a referenced row is deleted, the rows referencing
it can also be automatically deleted (cascading delete).
Example of Foreign Key
Consider two tables: Students and Enrollments.
Students Table
StudentID Name Email
1 Alice [email protected]
2 Bob [email protected]
Primary Key: StudentID
Courses Table
CourseID CourseName Credits
101 Math 3
102 Science 4
Primary Key: CourseID
Enrollments Table
EnrollmentID StudentID CourseID EnrollmentDate
1 1 101 2024-01-15
2 2 102 2024-01-16
3 1 102 2024-01-17
Primary Key: EnrollmentID
Foreign Keys: StudentID references Students(StudentID), CourseID references
Courses(CourseID)
Explanation
In the Enrollments table, StudentID is a foreign key that references StudentID in the
Students table. This ensures that every StudentID value in the Enrollments table
corresponds to an existing StudentID in the Students table.
Similarly, CourseID in the Enrollments table is a foreign key that references CourseID in
the Courses table.
Creating Foreign Keys in SQL
Here is how you can create these tables and define foreign keys in SQL:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100)
);
1.13De-normalization
De-normalization is the process of intentionally introducing redundancy into a database by
merging tables or adding redundant data. This is done to improve read performance at the
cost of potential update anomalies and increased storage requirements. De-normalization can
help in scenarios where read operations are more frequent than write operations and where
performance is critical.
Why De-normalize?
1. Performance Improvement: Reducing the number of joins needed to retrieve data can
significantly speed up query performance.
2. Simpler Queries: De-normalized tables can make queries simpler and faster to write and
execute.
3. Caching: De-normalized tables can be used for caching purposes to avoid repetitive complex
joins.
Examples of De-normalization
Example 1: Combining Tables
Normalized Tables:
Orders Table:
OrderID CustomerID OrderDate
1 1001 2024-01-01
2 1002 2024-01-02
Customers Table:
CustomerID CustomerName
1001 Alice
1002 Bob
De-normalized Orders Table:
OrderID CustomerID CustomerName OrderDate
1 1001 Alice 2024-01-01
2 1002 Bob 2024-01-02
In this example, the Orders table now includes the CustomerName from the Customers table,
eliminating the need for a join when retrieving order information along with customer names.
Example 2: Adding Redundant Data
Normalized Tables:
Sales Table:
SaleID ProductID Quantity
1 101 2
SaleID ProductID Quantity
2 102 3
Products Table:
ProductID ProductName Price
101 Widget 25
102 Gadget 15
De-normalized Sales Table:
SaleID ProductID ProductName Price Quantity
1 101 Widget 25 2
2 102 Gadget 15 3
Here, the Sales table includes ProductName and Price from the Products table, so there's
no need to join these tables to get complete sales information.
Advantages of De-normalization
1. Performance Gains: Faster read queries due to reduced number of joins.
2. Simpler Queries: Queries become easier to write and understand.
3. Efficiency: Can be more efficient for reporting and data retrieval purposes.
Disadvantages of De-normalization
1. Update Anomalies: Increased risk of data inconsistencies when updating redundant data.
2. Increased Storage: More storage space is required to store redundant data.
3. Complexity in Writes: More complex logic may be needed to ensure data consistency during
write operations.
When to Use De-normalization
High Read Operations: When the system experiences a high volume of read operations
compared to write operations.
Complex Queries: When complex joins significantly impact performance.
Data Warehousing: In data warehousing and reporting systems where data is often read-
heavy and less frequently updated.
1.14 Relational algebra.
Relational algebra is a formal system for manipulating relational databases. It consists of a set
of operations that take one or two relations as input and produce a new relation as output.
These operations are fundamental to the field of database theory and are used to query and
manipulate data stored in relational databases.
Basic Operations of Relational Algebra
1. Selection (σ)
2. Projection (π)
3. Union (∪)
4. Set Difference (-)
5. Cartesian Product (×)
6. Rename (ρ)
Advanced Operations
1. Intersection (∩)
2. Join (⨝)
3. Division (÷)
Basic Operations Explained
1. Selection (σ)
The selection operation selects rows from a relation that satisfy a given predicate.
Notation: σ<sub>condition</sub>(Relation)
Example: Select all students with age greater than 20.
σ Age > 20 (Students)
2. Projection (π)
The projection operation selects certain columns from a relation.
Notation: π<sub>columns</sub>(Relation)
Example: Select the names and ages of all students.
π Name, Age (Students)
3. Union (∪)
The union operation combines the tuples of two relations and removes duplicates.
Notation: Relation1 ∪ Relation2
Example: Combine two sets of students.
Students1 ∪ Students2
4. Set Difference (-)
The set difference operation finds tuples that are in one relation but not in the other.
Notation: Relation1 - Relation2
Example: Find students who are in Students1 but not in Students2.
Students1 - Students2
5. Cartesian Product (×)
The Cartesian product operation returns the Cartesian product of two relations.
Notation: Relation1 × Relation2
Example: Combine each student with each course.
Students × Courses
6. Rename (ρ)
The rename operation changes the name of a relation or its attributes.
Notation: ρ<sub>new_name</sub>(Relation) or
ρ<sub>new_name(columns)</sub>(Relation)
Example: Rename the Students relation to Pupils.
ρ Pupils (Students)
Advanced Operations Explained
1. Intersection (∩)
The intersection operation returns tuples that are in both relations.
Notation: Relation1 ∩ Relation2
Example: Find students who are in both Students1 and Students2.
Students1 ∩ Students2
2. Join (⨝)
The join operation combines related tuples from two relations into single tuples.
Notation: Relation1 ⨝<sub>condition</sub> Relation2
Example: Join Students and Enrollments on StudentID.
Students ⨝ Students.StudentID = Enrollments.StudentID Enrollments
Types of Joins:
Theta Join (⨝<sub>θ</sub>): Join based on a general condition θ.
Equi-Join: Join based on equality.
Natural Join (⨝): Join based on all common attributes.
3. Division (÷)
The division operation is used to find tuples in one relation that are associated with all tuples
in another relation.
Notation: Relation1 ÷ Relation2
Example: Find students who are enrolled in all courses.
Students(StudentID, CourseID) ÷ Courses(CourseID)
Example Use Case
Consider the following relations:
Students:
StudentID Name Age
1 Alice 21
2 Bob 22
Enrollments:
StudentID CourseID
1 101
2 102
1 102
Courses:
CourseID CourseName
101 Math
102 Science
Query: Find the names of students who are enrolled in the Science course.
1. Join Enrollments and Courses:
Enrollments ⨝ Enrollments.CourseID = Courses.CourseID Courses
2. Select rows where CourseName is 'Science':
σ CourseName = 'Science' (Enrollments ⨝ Enrollments.CourseID =
Courses.CourseID Courses)
3. Join with Students to get names:
Students ⨝ Students.StudentID = Enrollments.StudentID σ CourseName =
'Science' (Enrollments ⨝ Enrollments.CourseID = Courses.CourseID
Courses)
4. Project the Name column:
π Name (Students ⨝ Students.StudentID = Enrollments.StudentID σ
CourseName = 'Science' (Enrollments ⨝ Enrollments.CourseID =
Courses.CourseID Courses))
This sequence of operations uses relational algebra to retrieve the names of students enrolled
in the Science course.
2.2Data types
1. Numeric Data Types
Integer Types
TINYINT: A very small integer. Range: -128 to 127 (signed) or 0 to 255 (unsigned).
SMALLINT: A small integer. Range: -32,768 to 32,767 (signed) or 0 to 65,535 (unsigned).
MEDIUMINT: A medium-sized integer. Range: -8,388,608 to 8,388,607 (signed) or 0 to
16,777,215 (unsigned).
INT or INTEGER: A standard integer. Range: -2,147,483,648 to 2,147,483,647 (signed) or 0 to
4,294,967,295 (unsigned).
BIGINT: A large integer. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
(signed) or 0 to 18,446,744,073,709,551,615 (unsigned).
Floating-Point Types
FLOAT: A small (single-precision) floating-point number. Range: approximately
±1.175494351 E-38 to ±3.402823466 E+38.
DOUBLE: A normal-size (double-precision) floating-point number. Range: approximately
±2.2250738585072014 E-308 to ±1.7976931348623157 E+308.
DECIMAL: An exact fixed-point number. Allows for precise storage of decimal values. Syntax:
DECIMAL(M, D), where M is the maximum number of digits and D is the number of digits to
the right of the decimal point.
2. String Data Types
Character String Types
CHAR: A fixed-length string. Syntax: CHAR(N), where N is the length of the string (0 to 255).
VARCHAR: A variable-length string. Syntax: VARCHAR(N), where N is the maximum length of
the string (0 to 65,535).
Text Types
TINYTEXT: A very small text string. Maximum length: 255 characters.
TEXT: A small text string. Maximum length: 65,535 characters.
MEDIUMTEXT: A medium-length text string. Maximum length: 16,777,215 characters.
LONGTEXT: A large text string. Maximum length: 4,294,967,295 characters.
Binary Data Types
BINARY: A fixed-length binary string. Syntax: BINARY(N), where N is the length of the string.
VARBINARY: A variable-length binary string. Syntax: VARBINARY(N), where N is the
maximum length of the string.
Blob Types
TINYBLOB: A very small binary large object. Maximum length: 255 bytes.
BLOB: A small binary large object. Maximum length: 65,535 bytes.
MEDIUMBLOB: A medium-sized binary large object. Maximum length: 16,777,215 bytes.
LONGBLOB: A large binary large object. Maximum length: 4,294,967,295 bytes.
Enum and Set Types
ENUM: An enumeration. A string object that can have only one value, chosen from a list of
permitted values. Syntax: ENUM('value1', 'value2', ...).
SET: A set. A string object that can have zero or more values, each chosen from a list of
permitted values. Syntax: SET('value1', 'value2', ...).
3. Date and Time Data Types
DATE: A date value. Format: 'YYYY-MM-DD'. Range: '1000-01-01' to '9999-12-31'.
DATETIME: A date and time combination. Format: 'YYYY-MM-DD HH:MM
'. Range: '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
TIMESTAMP: A timestamp. Format: 'YYYY-MM-DD HH:MM
'. Range: '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
TIME: A time value. Format: 'HH:MM
'. Range: '-838:59:59' to '838:59:59'.
YEAR: A year value. Format: 'YYYY'. Range: 1901 to 2155 (4-digit year) or 70 to 69 (2-digit
year representing 1970 to 2069).
4. Spatial Data Types
GEOMETRY: A geometric object.
POINT: A point in 2D space.
LINESTRING: A series of points forming a line.
POLYGON: A polygon.
MULTIPOINT: A collection of points.
MULTILINESTRING: A collection of lines.
MULTIPOLYGON: A collection of polygons.
GEOMETRYCOLLECTION: A collection of geometry objects.
5. JSON Data Type
JSON: A JSON (JavaScript Object Notation) data type for storing JSON documents.
Examples
Creating a Table with Various Data Types
CREATE TABLE ExampleTable (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
age INT,
birth_date DATE,
registered_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
bio TEXT,
profile_image BLOB,
preferences JSON,
location POINT,
rating DECIMAL(3, 2)
);
This table includes various data types to demonstrate the flexibility of MySQL in handling
different kinds of data.
2.4 DDL
DDL stands for Data Definition Language, and it is a subset of SQL (Structured Query
Language) used to define and manage the structure of a database and its objects. DDL
statements are used to create, modify, and drop database objects such as tables, indexes,
views, and schemas. Here are the primary DDL statements in SQL:
1. CREATE
The CREATE statement is used to create database objects like tables, indexes, views, and
schemas.
Create a Table:
CREATE TABLE Students (
StudentID INT AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Age INT,
GPA DECIMAL(3, 2)
);
Create an Index:
CREATE INDEX idx_lastname ON Students(LastName);
Create a View:
CREATE VIEW StudentSummary AS
SELECT StudentID, FirstName, LastName, Age FROM Students WHERE GPA >=
3.5;
2. ALTER
The ALTER statement is used to modify the structure of an existing database object.
Alter Table (Add Column):
ALTER TABLE Students ADD COLUMN Email VARCHAR(100);
Alter Table (Modify Column):
ALTER TABLE Students MODIFY COLUMN Age INT UNSIGNED;
Alter Table (Drop Column):
ALTER TABLE Students DROP COLUMN GPA;
3. DROP
The DROP statement is used to delete database objects like tables, indexes, views, and
schemas.
Drop Table:
DROP TABLE Students;
Drop Index:
DROP INDEX idx_lastname ON Students;
Drop View:
DROP VIEW StudentSummary;
4. TRUNCATE
The TRUNCATE statement is used to remove all rows from a table, while keeping the table
structure intact.
Truncate Table:
TRUNCATE TABLE Students;
5. RENAME
The RENAME statement is used to rename an existing database object.
Rename Table:
RENAME TABLE Students TO NewStudents;
6. COMMENT ON
The COMMENT ON statement is used to add comments to database objects like tables and
columns.
Add Comment to Table:
COMMENT ON TABLE Students IS 'Table storing information about
students.';
Add Comment to Column:
COMMENT ON COLUMN Students.FirstName IS 'First name of the student.';
Example Usage
Let's go through an example scenario using some DDL statements:
-- Create a table
CREATE TABLE Employees (
EmployeeID INT AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100),
HireDate DATE
);
2.5 DML
"DML" stands for Data Manipulation Language. It is a subset of SQL (Structured Query
Language) used for managing data within relational databases. DML statements are used to
retrieve, insert, update, and delete data from database tables. Here are the primary DML
statements in SQL:
1. SELECT
The SELECT statement retrieves data from one or more tables.
Basic Select:
SELECT * FROM Employees;
Select with Conditions:
SELECT FirstName, LastName FROM Employees WHERE Department = 'IT';
Select with Joins:
SELECT e.FirstName, e.LastName, d.DepartmentName
FROM Employees e
INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID;
2. INSERT
The INSERT statement is used to add new rows of data into a table.
Insert into Table:
INSERT INTO Employees (FirstName, LastName, Email, HireDate,
Department)
VALUES ('Alice', 'Johnson', '[email protected]', '2023-03-
10', 'Finance');
3. UPDATE
The UPDATE statement modifies existing data in a table.
Update Records:
UPDATE Employees
SET Department = 'Marketing'
WHERE EmployeeID = 101;
4. DELETE
The DELETE statement removes rows from a table.
Delete Records:
DELETE FROM Employees
WHERE EmployeeID = 102;
Example Usage
Let's go through an example scenario using these DML statements:
-- Insert data into Employees table
INSERT INTO Employees (FirstName, LastName, Email, HireDate, Department)
VALUES ('John', 'Doe', '[email protected]', '2023-01-15', 'IT'),
('Jane', 'Smith', '[email protected]', '2022-05-20', 'HR');
2.6 DCL
"DCL" stands for Data Control Language in the context of databases. It is a subset of SQL
(Structured Query Language) used to control access to data stored in a database. DCL
statements primarily include commands for granting and revoking privileges and permissions
to database users. The two main DCL statements in SQL are:
1. GRANT
The GRANT statement is used to give specific privileges to database users or roles.
Grant SELECT privilege to a user:
sql
Copy code
GRANT SELECT ON Employees TO user1;
Grant INSERT, UPDATE privileges to a role:
sql
Copy code
GRANT INSERT, UPDATE ON Employees TO role1;
2. REVOKE
The REVOKE statement is used to revoke previously granted privileges from database users or
roles.
Revoke SELECT privilege from a user:
REVOKE SELECT ON Employees FROM user1;
Revoke all privileges from a user:
REVOKE ALL PRIVILEGES ON Employees FROM user1;
Example Usage
Let's illustrate the usage of GRANT and REVOKE statements with a simple scenario:
-- Create a new user
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'password';
-- Create a savepoint
SAVEPOINT sp1;
2.12 Other Operator: like, in, not, between, exists, all, any, is null, is not null, distinct.
Certainly! In SQL, there are various other operators and keywords that are used for different
purposes such as comparison, filtering, and checking conditions. Here's an explanation of
each of these operators and keywords:
1. LIKE
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
Basic Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;
Example:
SELECT * FROM Employees
WHERE FirstName LIKE 'J%';
This query retrieves all rows from the Employees table where the FirstName starts
with 'J'.
Wildcard Characters:
o %: Represents zero or more characters.
o _: Represents a single character.
2. IN
The IN operator allows you to specify multiple values in a WHERE clause.
Basic Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);
Example:
SELECT * FROM Products
WHERE Category IN ('Electronics', 'Clothing', 'Books');
This query retrieves all rows from the Products table where the Category is either
'Electronics', 'Clothing', or 'Books'.
3. NOT
The NOT operator reverses the result of a condition.
Example:
SELECT * FROM Employees
WHERE NOT Department = 'IT';
This query retrieves all rows from the Employees table where the Department is not
'IT'.
4. BETWEEN
The BETWEEN operator selects values within a specified range.
Basic Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example:
SELECT * FROM Orders
WHERE OrderDate BETWEEN '2023-01-01' AND '2023-12-31';
This query retrieves all rows from the Orders table where the OrderDate is between
January 1st, 2023 and December 31st, 2023.
5. EXISTS
The EXISTS operator is used in a WHERE clause to test for the existence of any rows in a
subquery.
Basic Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE EXISTS (subquery);
Example:
SELECT * FROM Customers
WHERE EXISTS (
SELECT 1 FROM Orders WHERE Orders.CustomerID =
Customers.CustomerID
);
This query retrieves all rows from the Customers table where there exists at least one
corresponding row in the Orders table for that customer.
6. ALL and ANY/IN
The ALL and ANY (synonymous with IN) operators are used with subqueries and comparison
operators (=, >, <, etc.).
Example with ALL:
SELECT * FROM Products
WHERE Price > ALL (SELECT AVG(Price) FROM Products GROUP BY
Category);
This query retrieves all products where the Price is greater than the average price of
products in each category.
Example with ANY or IN:
SELECT * FROM Orders
WHERE CustomerID = ANY (SELECT CustomerID FROM Customers WHERE
Country = 'USA');
This query retrieves all orders placed by customers from the USA.
7. IS NULL / IS NOT NULL
The IS NULL and IS NOT NULL operators are used to test for NULL values in columns.
Example:
SELECT * FROM Employees
WHERE Email IS NULL;
This query retrieves all rows from the Employees table where the Email column is
NULL.
8. DISTINCT
The DISTINCT keyword is used in a SELECT statement to eliminate duplicate rows from the
result set.
Example:
SELECT DISTINCT Department FROM Employees;
This query retrieves all unique Department values from the Employees table,
eliminating duplicates.
Example Usage in SQL Queries
Here are some scenarios demonstrating the use of these operators and keywords in SQL
queries:
-- Select customers whose name starts with 'A' or 'B'
SELECT * FROM Customers
WHERE CustomerName LIKE 'A%' OR CustomerName LIKE 'B%';
-- Select orders with an order total greater than any order total placed by
customer ID 123
SELECT * FROM Orders
WHERE OrderTotal > ANY (SELECT OrderTotal FROM Orders WHERE CustomerID =
123);