SQL (1) 2
SQL (1) 2
SQL
SQL
• Language developed by IBM (1970s) to manipulate
the data stored in database system
• Originally called “Structured English Query
Language” (SEQUEL), later it is called SQL
“Structured Query language”.
Syntax:
Create table <table name>
(<column name1> <datatype>(<size>),
<column name2> <datatype>(<size>)………
<column nameN> <datatype>(<size>));
Output:
Table Created
(2) Alter Command
For Example:
Alter table Student ADD ( Marks number(5))
Syntax:
Alter Table <Table Name> DROP Column <Column Name>;
For Example: Drop the column name Marks from the student
table
Syntax
Drop Table <Tablename>;
Insert Command
Syntax:
Output:
2 rows created
Insert Null Values
Syntax:
insert all
into Student(name, marks) values(‘John', 1)
into student (name, marks) values(‘Roy', 3)
into student(name, marks) values(‘ram', 4);
For Example:
Example:
Mysql
insert into table_name values(“val1”, val, val,(date(‘yyyy-mm-dd’)));
Or
insert into table_name(att, attr,…) values(“val1”, val, val,(date(‘yyyy-mm-dd’)));
Example:-
SET SQL_SAFE_UPDATES = 0;
update employee SET date = '2024-12-11' WHERE ID = 'S1';
update employee SET date = '2024-12-11' WHERE ID = 'S2';
update employee SET date = '2024-12-11' WHERE ID = 'S3';
update employee SET date = '2024-12-11' WHERE ID = 'S4';
Student
John Mumbai 2
Name
Rahul
Filtering Table Data
Selected columns and all rows
Syntax::
select <column1>, <column2> from <table name>;
Name City
John Mumbai
Rahul Delhi
Selected rows and all columns:- using where
clause
Retrieval of specific rows from a table
Syntax::
select * from <table name> where<condition>;
john Mumbai 2
Selected rows and selected columns:-
Retrieval of specific rows and specific column from a table
Syntax::
select <column1>, <column2> from <table name>
where<condition>;
Name City
John Mumbai
Updating a Table
SET SQL_SAFE_UPDATES = 0;
update employee SET date = '2024-12-11' WHERE ID = 'S1';
update employee SET date = '2024-12-11' WHERE ID = 'S2';
update employee SET date = '2024-12-11' WHERE ID = 'S3';
update employee SET date = '2024-12-11' WHERE ID = 'S4';
Delete Command
Delete is used to remove either:
Syntax:
1) DELETE from <table name>;
For Example: Delete all the records of those students who live
in Mumbai city.
Name
Rahul
Herry
John
Distinct Command
• Eliminating duplicate rows from the table.
Syntax:
Select DISTINCT * from <table name>;
For Example:
CREATE TABLE Employee ( E_Id number(10),
LastName varchar2(10) NOT NULL, FirstName varchar2(10)
UNIQUE, Address varchar2(10) not null UNIQUE, City
varchar2(10) );
For Example:
Create table Customer_master (Cust_id number(5) primary key,
address char(10) default ‘India’, loan_no number(6));
Create table emp_database (emp_no number(5) primary key,
name varchar2(10), designation varchar2(10) , C_no number
(10) References customer_master (Cust_id), salary number(5));
OR
Create table emp_database (emp_no Number(5) primary key,
name varchar2(10), designation varchar2(10) , C_no number
(10), salary number(5), foreign key (C_No) References
customer_master (Cust_id));
Create table student (id number(10) , name varchar(20), class char(20),
primary key(id));
Or
Create table course(Course_id number(10), c_name char(20), S_id
number(10), date_of_conduct date), foreign key (S_id) references
student(id))
For Example:
CREATE TABLE Employee ( E_Id number(10) Primary Key,
LastName varchar2(10) NOT NULL, FirstName varchar2(10), City
varchar2(10) default ‘London’);
Insert into Employee (E_Id, Lastname, FirstName) values (11,
‘John’, ‘Pareira’);
Output
Note: By default value will be London but we can also update the
value at the time of insert records.
Logical Operators
1. AND : It is used for creating SQL statement with two or more
conditions and all conditions must be satisfied.
• Subtraction (-)
• Division (/)
• Multiplication (*)
• Enclosed Operation ()
Range Searching (Between Operator)
• Retrieve data within range of values.
• The range coded after the word BETWEEN in inclusive and two
values in between the range must be linked with a keyword AND.
Description
Books
Pens
Bags
Pencils
Description
Folders
IN Operator
Order Operator
2 Comparison Operators
3 IN
4 BETWEEN
3. MIN
Operates on numbers and non numbers as well.
4. MAX
5. COUNT
SUM()
The sum of the values in a specified column.
Syntax:
Select SUM (column name) from <table name> where
<condition>; OR
Sum(salary) OR Total_Salary
29000 29000
AVG()
The average of the values in a specified column.
Syntax:
Select AVG (column name) from <table name> where
<condition>;
For Example: Select AVG (salary) from client ;
AVG(salary)
9.666666
Min()
It returns the smallest values in a specified column.
Syntax:
Select MIN (column name) from <table name> where
<condition>;
For Example: Select MIN (sell_price) from product ;
Min(marks)
9
Max()
It returns the maximum value in a specified column.
Syntax:
Select MAX (column name) from <table name> where
<condition>;
For Example: Select MAX (salary), Avg(salary) from client ;
Max(marks)
10
COUNT()
• Returns the Number of records in a specified column where
expression is not null.
Syntax:
Select COUNT (Column Name) from <Table Name>;
For Example:
Select COUNT (name) from Emp;
COUNT(name)
3
How many different job titles are stored in the relation EMP?
- select count(distinct JOB) from EMP;
COUNT(*)
• Returns the total Number of records in a specified column.
3 Herry Engineer
COUNT(*)
4 Null Null engineer
Set Operations
• SQL supports few SET Operations on data table.
• These functions are used to get meaningful results from
different conditions.
Syntax:
select <column name> from table name1 UNION select
<column name> from table name2
OR
select <column name> from table name 1 where condition
UNION select <column name> from table name 2 (or 1) where
condition
Client_ID Name Cust_ID Name
c001 Ivan c002 John
c002 John c003 William
CLIENT C004 Lara
Employee
select clinet_id, name from CLIENT UNION select cust_id, name from
employee
Client_ID Name
c001 Ivan
c002 John
C003 Willaim
c004 Lara
select client_id, name From CLIENT UNION ALL select clust_id, name from
CUSTOMER
Client_ID Name
c001 Ivan
c002 John
C002 John
c003 William
c004 Lara
Set Operation: INTERSECT
• It is used to combine two select statement or tables and return
the common result.
Syntax:
select <column name> from table name1 INTERSECT select
<column name> from table name2
OR
select <column name> from table name 1 where condition
INTERSECT select <column name> from table name 2 where
condition
Client_ID Name Cust_ID Name
c001 Ivan c002 John
c002 John c003 William
C004 Lara
CLIENT
Employee
select clinet_id, name from CLIENT INTERSECT select cust_id, name from
Employee
Client_ID Name
c002 John
Set Operation: EXCEPT OR MINUS
• Combine results of two select statement and return only those
results which is in first set.
Syntax:
select <column name> from table name1 minus select <column
name> from table name2
OR
select <column name> from table name 1 where condition minus
select <column name> from table name 2 where condition
Client_ID Name Cust_ID Name
c001 Ivan c002 John
c002 John c003 William
C004 Lara
CLIENT
CUSTOMER
select clinet_id, name from CLIENT Minus select cust_id, name from
CUSTOMER
Client_ID Name
c001 Ivan
GROUP BY CLAUSE
To displays avg salary of employees work for each
department?
SELECT Avg (salary) “Avg salary“, dpt_id
FROM employee
GROUP BY dpt_ID;
Example:
Syntax:
SELECT column1, column2 FROM table WHERE [ conditions ] GROUP BY column
HAVING [ conditions ] ORDER BY column
To display employee department no, total no. of departments,
max and min salary of each department having max salary
greater than 3
SELECT dpt_number "Department",
COUNT(*) "Department Count",
MAX(emp_salary) "Top Salary",
MIN(emp_salary) "Low Salary“
FROM employee
GROUP BY dpt_number
HAVING max (salary) >= 3;
To return the name of the department and the total sales of the
associated department with product total sales greater than
25,000
• The SQL LIKE condition can be used in any valid SQL statement :
Select, Insert, Update and Delete
• Return the employee records whose salary is of 4 digits and starting with 57
Select * from employee where salary LIKE ‘57__’;
EMP-ID NAME SALARY
1002 Jane Anderson 5750
• Retrieve the customers records whose names do not start with ‘J’
select Cust_ID, Name from customer where Name Not LIKE ‘J%’ ;
d) SYSDATE
20
9
3. ROUND: Returns rounded value
Syntax: ROUND(n[m])
Select ROUND(23.66, 1) “ROUND” from DUAL;
Round
---------------------------
23.7
LEAST
----------------------
16
125.3
9. FLOOR: Returns the integer value that is equal to or less than number
Syntax: FLOOR(n)
Select FLOOR(24.8) “FLOOR” from DUAL;
FLOOR
-------------------------
24
10. CEIL: Returns the integer value that is equal to or greater than number
Syntax: CEIL(n)
Select CEIL(24.8) “ceil” from DUAL;
ceil
-----------------
25
Syntax:
Example:
Output: 21-09-13
To_Date ()
• It converts character values into date value
Example:
Output: 21-SEP-13
Creating Table from a Table
Syntax:
For Example: insert into Course select name, city, marks from student
where marks=10;
VIEWS in SQL
• A view is a virtual table contains rows and columns, just like a real
table. The fields in a view are fields from one or more real tables in
the database.
Syntax:
CREATE VIEW view_name AS SELECT column_name(s) FROM
table_name WHERE condition
For Example:
Create View product_vw AS select description from product
where city = ‘Mumbai’ Order by city
Syntax:
Select column(s) from VIEW;
For Example:
Select description from product_vw;
To Delete VIEW:
Drop View <View Name>
1) Natural Join
• Inner Join
• Equi Join
2) Outer Join
• Left Outer Join
• Right Outer Join
• Full Outer Join
• Tables are joined on columns that have same datatype and size
Inner Join
• It is a natural join used for retrieving records from more than two
tables having common data
• Inner join can be Equi join if it is having equal condition only
Syntax:
Employee
Dept
Table 1 Table 2
Resultant
Table
Emp_No Name Branch_No
11 Smith B001
List out the employee details
23 William B007
along with their branch name
34 Simi B007
using Left Outer Join
Employee
Branch_Name Branch_ Select e.emp_no, e.name,
No
b.branch_name from Employee e LEFT
Mumbai B001 OUTER JOIN Branch b ON e. branch_no
Mumbai B007 = b. branch no
Pune C006
Banglore F007
Branch
Right Outer Join
• The Right JOIN keyword returns all rows from the right table
(table2), with the matching rows in the left table (table1).
• The result is NULL in the right side when there is no match.
Table 1 Table 2
Resultant
Table
Emp_No Name Branch_No
11 Smith B001
List out the employee details
23 William B007
along with their branch name
34 Simi C006
using Right Outer Join
Employee
Select e.emp_no, e.name,
Branch_Name Branch_ b.branch_name from Employee e
No RIGHT OUTER JOIN Branch b ON e.
Mumbai B001 branch_no = b. branch no
Mumbai B007
Pune C009
Banglore F007
Branch
Full Outer Join
• It does both of those operations, padding tuples from the left
relation that did not match any from the right relation
• As well as tuples from the right relation that did not match any
from the left relation and adding them to the result of the join.
Table 2
Table 1
Resultant
Table
Emp_No Name Branch_No
11 Smith B001
List out the employee details
23 William B007
along with their branch name
34 Simi C006
using Full Join
Employee
Branch_Name Branch_
Select e.emp_no, e.name,
No b.branch_name from Employee e
Mumbai B001 FULL OUTER JOIN Branch b ON e.
Mumbai B007
branch_no = b. branch no
Pune C009
Banglore F007
Branch
SUBQUERIES
• A subquery is a form of SQL statement that appears inside
another SQL statement
• Subquery also termed as Nested Queries
• First table is called Parent statement which uses rows returned by
subqueries