SlideShare a Scribd company logo
Er. Nawaraj Bhandari
Topic 8
SQL 2
Data Definition - 1
Create table departments
(dept_no integer not null,
department_name varchar(30),
location varchar2(3)
primary key dept_no);
Data Definition - 2
Create table departments
(dept_no integer not null,
department_name varchar(30),
location varchar2(3)
primary key dept_no);
The table name
Here the columns
are defined. The
datatypes
are specified along
with the length in
brackets.
If a column is
specified as NOT
NULL, then it
is mandatory and
must be populated
when a
new row is
created.
Primary Key
Create table departments
(dept_no integer not null,
department_name
varchar(30),
location varchar2(3)
primary key dept_no);
Primary Key
defined in
different
places
Create table departments
(dept_no integer not null primary
key,
department_name varchar(30),
location varchar(3));
Foreign Key
Create table workers
(emp_no number(5) not null,
first_name varchar(30),
last_name varchar(30),
job_title varchar(30),
age number(3),
dept_no number(5),
primary key emp_no,
foreign key (dept_no) references departments(dept_no));
Modifying Tables Using SQL
 Add an extra column
 Drop a column from a table
 Modify the maximum length of the table
 Add a new constraint
 Drop a constraint
 Set a default for a column
 Drop a default for a column
Example of Adding a Column Using
Alter Table
ALTER TABLE job_type
ADD salary FLOAT;
Data Manipulation
 Select
 Order By
 Aggregate functions
 Group by
 Subqueries
 Joins
Select
 SELECT first_name
 FROM Students
 WHERE Student_type = ‘Overseas’;
All the Columns or Some of Them
 Select *
 from Students
 Where student_type = ‘Overseas’;
 Select student_id, first_name, last_name,
 From Students
 Where student_type = ‘Overseas’;
Order By - 1
 Select first_name, last_name, stu_id
 From Students
 Where student_type = ‘Overseas’
 Order by last_name;
Order By - 2
 Select first_name, last_name, stu_id
 From Students
 Where student_type = ‘Overseas’
 Order by last_name desc;
Aggregate Functions - 1
 Count
 Sum
 Avg
 Min
 Max
Aggregate Functions - 2
 Count – returns number of values in a column
 Sum – returns the sum total of values of a column
 Avg – returns the mean average of values in column
 Min – returns the lowest value in a column
 Max – returns the highest value in a column
Example of Aggregate Function
Select branchID, Count(staff_id)
From workers
Where branchType = ‘Main’
Group by branchID
Having Count (staff_id) > 1
Order by branchID
• This counts the number of members of staff in main
branches where there are more than 1 staff member.
It groups them by the branchID.
Group By
 As shown it the previous slide...
 This clause is used with an aggregate function and groups the results
by some attribute.
Having Clause
 Modifies the group by clause
Select branchID, Count(staff_id)
From workers
Where branchType = ‘Main’
Group by branchID
Having Count (staff_id) > 1
Order by branchID
 In this case, only select groups where it has been calculated by the
count function that there are more than one member of staff.
Sub-Queries
Select d.department_name, d.location
From departments d, workers w
Where d.dept_no = w.dept_no
And w.age =
(select max(w2.age)
From workers w2);
Joins
Select d.department_name, w.first_name, w.last_name
From departments d, workers w
Where d.dept_no = w.dept_no;
Fixing Errors
 Not specifying join condition properly
 Syntax errors
 Spelling errors for keywords
 Columns not existing on tables
 Data-types being mixed up
Query Optimisation
 Making sure a query runs as efficiently and as quickly as possible
 Performance
 Examining path a query takes through a database
TOAD
 Tools that are used to help optimise queries
 Available at http://www.quest.com/toad/
Just Do It!
 Like driving a car...
 The best way to become proficient with SQL is to use it.
 Laboratory sessions
 Assignment
Learning Outcomes – Have We Met Them?
 By the end of this topic, students will be able to:
 Understand the syntax of the create statement
 Understand the construction of more complex selections
 Recognise the issues around error messaging and query optimisation
References
 Benyon-Davis, P. (2003). Database Systems, 3rd
edition. Palgrave Macmillan. Chapters 11, 12 & 13.
 Connolly, T. & Begg, C. (2004). Database Systems: A
Practical Approach to Design, Implementation, and
Management, 4th Edition. Addison Wesley. Chapters 5,
6 & 7.
 Dietrich, S. W. (2001). Understanding Relational
Database Query Languages, 1st edition. Prentice Hall.
Chapter 5.
 TOAD website http://www.quest.com/toad/
ANY QUESTIONS?

More Related Content

What's hot

Database Basics and MySQL
Database Basics and MySQLDatabase Basics and MySQL
Database Basics and MySQL
Jerome Locson
 
Dbms Interview Question And Answer
Dbms Interview Question And AnswerDbms Interview Question And Answer
Dbms Interview Question And Answer
Jagan Mohan Bishoyi
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
Chirag vasava
 
Database
DatabaseDatabase
SQL interview questions by jeetendra mandal - part 4
SQL interview questions by jeetendra mandal - part 4SQL interview questions by jeetendra mandal - part 4
SQL interview questions by jeetendra mandal - part 4
jeetendra mandal
 
Sql and Sql commands
Sql and Sql commandsSql and Sql commands
Sql and Sql commands
Knowledge Center Computer
 
SQL commands
SQL commandsSQL commands
SQL commands
GirdharRatne
 
DDL And DML
DDL And DMLDDL And DML
DDL And DML
pnp @in
 
Relational Database Management System part II
Relational Database Management System part IIRelational Database Management System part II
Relational Database Management System part II
KavithaA19
 
Sql fundamentals
Sql fundamentalsSql fundamentals
Sql fundamentals
Ravinder Kamboj
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
Nishant Munjal
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)ppt
Gowarthini
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
rainynovember12
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
Dr.Neeraj Kumar Pandey
 
Data Retrival
Data RetrivalData Retrival
Data Retrival
Er. Nawaraj Bhandari
 

What's hot (19)

Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Database Basics and MySQL
Database Basics and MySQLDatabase Basics and MySQL
Database Basics and MySQL
 
Introduction to mysql part 1
Introduction to mysql part 1Introduction to mysql part 1
Introduction to mysql part 1
 
Dbms Interview Question And Answer
Dbms Interview Question And AnswerDbms Interview Question And Answer
Dbms Interview Question And Answer
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
 
Database
DatabaseDatabase
Database
 
SQL interview questions by jeetendra mandal - part 4
SQL interview questions by jeetendra mandal - part 4SQL interview questions by jeetendra mandal - part 4
SQL interview questions by jeetendra mandal - part 4
 
Sql and Sql commands
Sql and Sql commandsSql and Sql commands
Sql and Sql commands
 
SQL commands
SQL commandsSQL commands
SQL commands
 
DDL And DML
DDL And DMLDDL And DML
DDL And DML
 
12 SQL
12 SQL12 SQL
12 SQL
 
Relational Database Management System part II
Relational Database Management System part IIRelational Database Management System part II
Relational Database Management System part II
 
Sql fundamentals
Sql fundamentalsSql fundamentals
Sql fundamentals
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)ppt
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
 
Data Retrival
Data RetrivalData Retrival
Data Retrival
 

Similar to SQL

Sql 
statements , functions & joins
Sql 
statements , functions  &  joinsSql 
statements , functions  &  joins
Sql 
statements , functions & joins
baabtra.com - No. 1 supplier of quality freshers
 
Dbms question
Dbms questionDbms question
Dbms question
Ricky Dky
 
interenship.pptx
interenship.pptxinterenship.pptx
interenship.pptx
Naveen316549
 
DDL and DML statements.pptx
DDL and DML statements.pptxDDL and DML statements.pptx
DDL and DML statements.pptx
Karthick Panneerselvam
 
SQL200.2 Module 2
SQL200.2 Module 2SQL200.2 Module 2
SQL200.2 Module 2
Dan D'Urso
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docxINFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
carliotwaycave
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2jomerson remorosa
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUESPYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
vanithasivdc
 
Micro project project co 3i
Micro project project co 3iMicro project project co 3i
Micro project project co 3i
ARVIND SARDAR
 
DIRECTIONS READ THE FOLLOWING STUDENT POST AND RESPOND EVALUATE I.docx
DIRECTIONS READ THE FOLLOWING STUDENT POST AND RESPOND EVALUATE I.docxDIRECTIONS READ THE FOLLOWING STUDENT POST AND RESPOND EVALUATE I.docx
DIRECTIONS READ THE FOLLOWING STUDENT POST AND RESPOND EVALUATE I.docx
lynettearnold46882
 
Project3
Project3Project3
Project3
ARVIND SARDAR
 
Database object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab AssignmentDatabase object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab Assignment
Arun Sial
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
Rai University
 

Similar to SQL (20)

Sql 
statements , functions & joins
Sql 
statements , functions  &  joinsSql 
statements , functions  &  joins
Sql 
statements , functions & joins
 
Dbms question
Dbms questionDbms question
Dbms question
 
Sql statement,functions and joins
Sql statement,functions and joinsSql statement,functions and joins
Sql statement,functions and joins
 
interenship.pptx
interenship.pptxinterenship.pptx
interenship.pptx
 
DDL and DML statements.pptx
DDL and DML statements.pptxDDL and DML statements.pptx
DDL and DML statements.pptx
 
SQL200.2 Module 2
SQL200.2 Module 2SQL200.2 Module 2
SQL200.2 Module 2
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docxINFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUESPYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
 
Dbms sql-final
Dbms  sql-finalDbms  sql-final
Dbms sql-final
 
003.query
003.query003.query
003.query
 
Sql
SqlSql
Sql
 
PPT
PPTPPT
PPT
 
Micro project project co 3i
Micro project project co 3iMicro project project co 3i
Micro project project co 3i
 
DIRECTIONS READ THE FOLLOWING STUDENT POST AND RESPOND EVALUATE I.docx
DIRECTIONS READ THE FOLLOWING STUDENT POST AND RESPOND EVALUATE I.docxDIRECTIONS READ THE FOLLOWING STUDENT POST AND RESPOND EVALUATE I.docx
DIRECTIONS READ THE FOLLOWING STUDENT POST AND RESPOND EVALUATE I.docx
 
Project3
Project3Project3
Project3
 
Database object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab AssignmentDatabase object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab Assignment
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 

Recently uploaded

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 

Recently uploaded (20)

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 

SQL

  • 2. Data Definition - 1 Create table departments (dept_no integer not null, department_name varchar(30), location varchar2(3) primary key dept_no);
  • 3. Data Definition - 2 Create table departments (dept_no integer not null, department_name varchar(30), location varchar2(3) primary key dept_no); The table name Here the columns are defined. The datatypes are specified along with the length in brackets. If a column is specified as NOT NULL, then it is mandatory and must be populated when a new row is created.
  • 4. Primary Key Create table departments (dept_no integer not null, department_name varchar(30), location varchar2(3) primary key dept_no); Primary Key defined in different places Create table departments (dept_no integer not null primary key, department_name varchar(30), location varchar(3));
  • 5. Foreign Key Create table workers (emp_no number(5) not null, first_name varchar(30), last_name varchar(30), job_title varchar(30), age number(3), dept_no number(5), primary key emp_no, foreign key (dept_no) references departments(dept_no));
  • 6. Modifying Tables Using SQL  Add an extra column  Drop a column from a table  Modify the maximum length of the table  Add a new constraint  Drop a constraint  Set a default for a column  Drop a default for a column
  • 7. Example of Adding a Column Using Alter Table ALTER TABLE job_type ADD salary FLOAT;
  • 8. Data Manipulation  Select  Order By  Aggregate functions  Group by  Subqueries  Joins
  • 9. Select  SELECT first_name  FROM Students  WHERE Student_type = ‘Overseas’;
  • 10. All the Columns or Some of Them  Select *  from Students  Where student_type = ‘Overseas’;  Select student_id, first_name, last_name,  From Students  Where student_type = ‘Overseas’;
  • 11. Order By - 1  Select first_name, last_name, stu_id  From Students  Where student_type = ‘Overseas’  Order by last_name;
  • 12. Order By - 2  Select first_name, last_name, stu_id  From Students  Where student_type = ‘Overseas’  Order by last_name desc;
  • 13. Aggregate Functions - 1  Count  Sum  Avg  Min  Max
  • 14. Aggregate Functions - 2  Count – returns number of values in a column  Sum – returns the sum total of values of a column  Avg – returns the mean average of values in column  Min – returns the lowest value in a column  Max – returns the highest value in a column
  • 15. Example of Aggregate Function Select branchID, Count(staff_id) From workers Where branchType = ‘Main’ Group by branchID Having Count (staff_id) > 1 Order by branchID • This counts the number of members of staff in main branches where there are more than 1 staff member. It groups them by the branchID.
  • 16. Group By  As shown it the previous slide...  This clause is used with an aggregate function and groups the results by some attribute.
  • 17. Having Clause  Modifies the group by clause Select branchID, Count(staff_id) From workers Where branchType = ‘Main’ Group by branchID Having Count (staff_id) > 1 Order by branchID  In this case, only select groups where it has been calculated by the count function that there are more than one member of staff.
  • 18. Sub-Queries Select d.department_name, d.location From departments d, workers w Where d.dept_no = w.dept_no And w.age = (select max(w2.age) From workers w2);
  • 19. Joins Select d.department_name, w.first_name, w.last_name From departments d, workers w Where d.dept_no = w.dept_no;
  • 20. Fixing Errors  Not specifying join condition properly  Syntax errors  Spelling errors for keywords  Columns not existing on tables  Data-types being mixed up
  • 21. Query Optimisation  Making sure a query runs as efficiently and as quickly as possible  Performance  Examining path a query takes through a database
  • 22. TOAD  Tools that are used to help optimise queries  Available at http://www.quest.com/toad/
  • 23. Just Do It!  Like driving a car...  The best way to become proficient with SQL is to use it.  Laboratory sessions  Assignment
  • 24. Learning Outcomes – Have We Met Them?  By the end of this topic, students will be able to:  Understand the syntax of the create statement  Understand the construction of more complex selections  Recognise the issues around error messaging and query optimisation
  • 25. References  Benyon-Davis, P. (2003). Database Systems, 3rd edition. Palgrave Macmillan. Chapters 11, 12 & 13.  Connolly, T. & Begg, C. (2004). Database Systems: A Practical Approach to Design, Implementation, and Management, 4th Edition. Addison Wesley. Chapters 5, 6 & 7.  Dietrich, S. W. (2001). Understanding Relational Database Query Languages, 1st edition. Prentice Hall. Chapter 5.  TOAD website http://www.quest.com/toad/