Chapter 3DMS
Chapter 3DMS
What is SQL?
• SQL is Structured Query Language, which is a computer language for
storing, manipulating and retrieving data stored in a relational database.
• SQL is the standard language for Relational Database System. All the
Relational Database Management Systems (RDMS) like MySQL, MS
Access, Oracle, Sybase, Informix, Postgres and SQL Server use SQL
as their standard database language.
Why SQL?
DML Commands:
SQL> SELECT * FROM Customers; (Selects all the columns from the
"Persons" table)
1. Grant and
2. Revoke.
1. Grant: -
Syntax: -
GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
Where,
• Privilege_name is the access right or privilege granted to the user. Some of
the access rights are ALL, EXECUTE, SELECT, ALTER, DELETE,
INSERT, and UPDATE.
• Object_name is the name of a database object like TABLE, VIEW,
STORED PROC and SEQUENCE.
• User_name is the name of the user to whom an access right is being
granted.
• PUBLIC is used to grant access rights to all users.
• ROLES are a set of privileges grouped together.
• WITH GRANT OPTION - allows a user to grant access rights to other
users.
2. Revoke:-
• The revoke command removes user access rights or privileges to the
database objects.
2. ROLLBACK: -
• This command restores the database to last committed state.
• The ROLLBACK command is used to undo transactions that have
not already been saved to the database.
• The ROLLBACK command can only be used to undo transactions
since the last COMMIT or ROLLBACK command was issued.
• We can either rollback the entire transaction or till a particular save
point transaction can be rolled back.
• The syntax for ROLLBACK is:
ROLLBACK TO SAVEPOINT_NAME; OR
ROLLBACK;
• Example: - ROLLBACK TO sv1; OR ROLLBACK;
3. SAVEPOINT: -
• Savepoint command is used to temporarily save a transaction so that
you can rollback to that point whenever necessary.
• Syntax: SAVEPOINT <Save_Point_Name>
• Example: - SAVEPOINT A;
SELECT DISTINCT
WHERE Clause
• The WHERE clause is used to filter records.
• The WHERE clause is used to extract only those records that
fulfill a specified condition.
• Syntax: - SELECT column1, column2... FROM table_name
WHERE condition;
• Example:- SELECT * FROM Customers WHERE
CustomerID=1;
SQL Operators
Arithmetic Operators (Q. List and explain any 4
arithmetic operators in SQL with example.)
• Arithmetic operators are used to perform mathematical
functions in SQL—the same as in most other languages.
There are four conventional operators for mathematical
functions:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
Operator Description Example
+ (addition) Addition is performed SELECT SALARY + BONUS
through the use of the FROM EMP;
plus (+) symbol.
- (subtraction) Subtraction is performed SELECT SALARY - BONUS
using the minus FROM EMP;
(-) symbol.
* (multiplication) Multiplication is SELECT SALARY * 10 FROM
performed by using the EMP;
asterisk (*) symbol.
/ (division) Division is performed SELECT SALARY / 10 FROM
through the use of the EMP;
―/‖ symbol.
Comparison Operators:-
Operator Description Example
= Checks if the values of two operands (a = b) is not true.
are equal or not, if yes then condition
becomes true.
!= Checks if the values of two operands (a != b) is true.
are equal or not, if values are not
equal then condition becomes true.
<> Checks if the values of two operands (a <> b) is true.
are equal or not, if values are not
equal then condition becomes true.
> Checks if the value of left operand is (a > b) is not true.
greater than the value of right
operand, if yes then condition
becomes true.
< Checks if the value of left operand is (a < b) is true.
less than the value of right operand, if
yes then condition becomes true.
>= Checks if the value of left operand is (a >= b) is not true.
greater than or equal to the value of
right operand, if yes then condition
becomes true.
<= Checks if the value of left operand is (a <= b) is true.
less than or equal to the value of right
operand, if yes then condition
becomes true.
!< Checks if the value of left operand is (a !< b) is false.
not less than the value of right
operand, if yes then condition
becomes true.
!> Checks if the value of left operand is (a !> b) is true
not greater than the value of right
operand, if yes then condition
becomes true.
Example 1. SQL> SELECT * from customers where salary >15000;
SQL> Select *from customers where PersonID=15;
Logical Operators:
Student:
StudentCourse :
Both these tables are connected by one common key (column) i.e
ROLL_NO.
We can perform a JOIN operation using the given SQL query:
SELECT s.roll_no, s.name, s.address, s.phone, s.age, sc.course_id
FROM Student s
JOIN StudentCourse sc ON s.roll_no = sc.roll_no;
Output:
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
• NATURAL JOIN
• SQL INNER JOIN
• The INNER JOIN keyword selects all rows from both the tables as
long as the condition is satisfied. This keyword will create the result-
set by combining all rows from both the tables where the condition
satisfies i.e value of the common field will be the same.
• Syntax:
• The syntax for SQL INNER JOIN is:
• SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
•
SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE FROM
Student
INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
LEFT JOIN
SQL LEFT JOIN
LEFT JOIN returns all the rows of the table on the left side of the join and
matches rows for the table on the right side of the join. For the rows for which
there is no matching row on the right side, the result-set will contain null. LEFT
JOIN is also known as LEFT OUTER JOIN.
Syntax
The syntax of LEFT JOIN in SQL is:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
HARSH 1
PRATIK 2
RIYANKA 2
DEEP 3
SAPTARHI 1
DHANRAJ NULL
ROHIT NULL
NIRAJ NULL
NULL 4
NULL 5
NULL 4
Nested Query
In SQL, a nested query involves a query that is placed within another query.
Output of the inner query is used by the outer query. A nested query has two
SELECT statements: one for the inner query and another for the outer query.
Correlated subqueries are executed once for each row of the outer query. They
use values from the outer query to return results.
Examples
1 1 1000
2 2 2000
3 3 3000
4 1 4000
5 5 5000
6 3 6000
7 2 7000
Example 1: Find the names of all employees in the Sales department.
Output
emp_name
John
Bob
Tom
Example 2: Find the names of all employees who have made a sale
Required query
Output
emp_name
John
Mary
Bob
Alice
Tom
view