0% found this document useful (0 votes)
7 views

6 Basic SQL

Uploaded by

yamauchis184
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

6 Basic SQL

Uploaded by

yamauchis184
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Chapter 4

Basic SQL
Company Database Schema
SQL Overview
• It was originally developed by IBM in 1970
• It is the standard relational database language
• It stands for Standard Query Language
• It is non-procedural language, you specify what information
needed rather than how to get it
• It has two types:
 Data Definition Language (DDL): Used it to define the structure
of the database. Define database, tables, attributes, datatypes,
constraints… Keywords: create, alter and drop

 Data Manipulation Language (DML): Used to manipulate data in


the database. Keywords: select, update, delete and insert
Basic SQL Retrieval Queries
Structure
Select <Attribute list>
From <Table list>
Where <Condition>

Example:
Select SSN, Fname, DOB
From Employee
Where salary > 100,000
Retrieve all Attributes, all
Rows
• Retrieve all the attributes and rows from the table:
• Select * from employee
• Retrieve some attributes for all employees:
• Select SSN, Fname, Lname, Salary from employee
• Retrieve some attributes for some employees:
• Select SSN, Fname, Dnumber from employee where
salary=2000
• Select SSN, Fname, DOB from employee where
salary=2000 and dnumber=3
Aliases for tables and
attributes
• Alias for attributes:
• Select Fname as First_Name, Lname as Last_Name from
Employee
• Alias for Tables:
• Select Fname as First_Name from employee as Emp
• Attribute reference by table name:
• Select employee.fname as First_name from employee
• Select emp.fname from employee as emp
Ordering of the retrieved
tuples
• Order of resulted rows without condition
• Select * from employee order by SSN
• Order of resulted rows with condition
• Select * from employee where salary<3000 order by SSN
• Order the resulted rows with different attributes
• Select SSN, Fname, address from employee order by salary, Dno
• Default ordering is ascending if descending then:
• Select * from employee order by salary desc
• Select * from employee order by salary desc, dno asc
Retrieve Distinct Values

• Select salary from employee (Query 1)


• Select distinct (salary) from employee (Query 2)

Query Salary Query


1 3000 2 Salary
2500 3000
3000
2500
3000
1200
2500
1200
Exercises:
• Retrieve project data for projects in department 10
• Select * from project where dnum=10
• Retrieve SSN for employees working in project number 1 with
hours greater than 10 hours
• Select ESSN from works_on where Pno=1 and hours> 10
• Retrieve SSN and Pno for employees working in either project
number 1 or 2 ordered by hours in descending way
• Select ESSN, Pno from works_on where pno=1 or pno=2 order
by hours desc
Select from Two Relations
• Retrieve project name and number along with the department
name controlling it

Select Pname, Pnumber,Dname from project join Department


On dnumber=dnum
Select from Two Relations
• Retrieve project name and SSN of employee working more
than 10 hours in this project
• Select Pname, ESSN from works_on join project on
project.Pnumber=works_on.Pno where hours >10
Try it Yourself …..
• Retrieve each department and its location
• Select Dname, Dlocation from department join dept_locations on
department.dnumber=dept_locations.dnumber

• Retrieve each department and its location with manager


SSN= 333445555
• Select Dname, Dlocation from department join dept_locations on
department.dnumber=dept_locations.dnumber where
mgr_ssn=333445555
Try it Yourself …..

• Retrieve employee name with his/her son or daughter data

• Select Fname,Lname, dependent_name, sex, bdate,


relationship from employee join dependent on ssn=essn
where relationship=‘son’ or relationship=‘daughter’
Try it Yourself …..

• Retrieve employee name and salary with his/her department


name

• Select Fname, Lname, salary, dname from employee join department on


dno=dnumber
Select from Different
Relations

• Retrieve department name and its locations and the name of its
manager with salary greater than 40000
Select Dname, dlocation,Fname+lname as manager_name from
employee join department on ssn=mgr_ssn join dept_locations on
department.dnumber =dept_locations.dnumber where salary> 40000
• Retrieve each department name and its location along with the
project name and location they manage

• Select dname, dloaction, pname, plocation


from department join dept_locations on
department.dnumber=
dept_locations.dnumber join project on
department.dnumber=project.dnum
• Retrieve each department name and its location
along with the project name and location they
manage provided that the department and project
are in the same location
• Select dname, dloaction, pname, plocation
from department join dept_locations on
department.dnumber=
dept_locations.dnumber join project on
department.dnumber=project.dnum Where
dlocation=plocation
• Retrieve employee name who have dependents and are
working in Administration department

Select fname, lname


from employee join dependent on ssn=essn
join department on dnumber=dno
where dname=‘ Administration’
Left and Right outer join
• Left outer join includes all rows even unmatched rows from the
left table written in the join clause
• Right outer join includes all rows even unmatched rows from
the right table written in the join clause
Dept_no Dept_nam Student_ID St_name Dept_no
e 100 Noha 1
1 IS
200 Bashayer 1
2 CS
300 Shahd 2
3 IT
Left and Right outer join
Dept_no Dept_nam Student_ID St_name Dept_no
e
100 Noha 1
1 IS
200 Bashayer 1
2 CS
300 Shahd 2
3 IT
• Select Dept_name, st_name from department left join student
on department.dept_no=student.dept_no

St_name Dept_no
Noha IS
Bashayer IS
Shahd CS
NULL IT
Left and Right outer join
Dept_no Dept_nam Student_ID St_name Dept_no
e
100 Noha 1
1 IS
200 Bashayer 1
2 CS
300 Shahd NULL
3 IT
• Select Dept_name, st_name from department right join student
on department.dept_no=student.dept_no

St_name Dept_no
Noha IS
Bashayer IS
Shahd NULL
Cross Join or Cartesian
Product
• In this type of join you don’t specify a join condition
• Unlike Inner, left, right outer join, cross join doesn’t
have On clause
• The number of rows resulted from cross join R1 and
R2 is R1x R2
• Syntax
 Select * from R1 cross join R2
Cross Join or Cartesian
Product
Department Student
Dept_no Dept_nam Student_ID St_name Dept_no
e 100 Noha 1
1 IS
200 Bashayer 1
2 CS
300 Shahd 2

Result of the cross join of the St_name Dept_nam


following query: e
Noha IS
Select st_name, Dept_name from Noha CS
student cross join department Bashayer IS
Bashayer CS
Shahd IS
Self Join
as Emp

Employee as manager

Retrieve each employee name and his/her


supervisor name
Select emp.fname as employee_name,
manager.fname as manager_name from employee as
emp join employee as manager On emp.super_ssn =
manager.ssn

You might also like