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

PPTX
Database Architecture
PPTX
Physical Design and Development
PPTX
Physical Design and Development
PPTX
Data Retrival
PPTX
DBMS Notes: DDL DML DCL
PPTX
Data concepts
Database Architecture
Physical Design and Development
Physical Design and Development
Data Retrival
DBMS Notes: DDL DML DCL
Data concepts

What's hot (19)

PPTX
Sql Basics And Advanced
PPTX
Structured query language(sql)ppt
PPTX
Supporting Transactions
PDF
Sql commands
PPTX
Chapter 1 introduction to sql server
PDF
Sql 2009
PDF
Introduction to Data Science With R Notes
PPT
Sql intro & ddl 1
PPTX
Islamic University Previous Year Question Solution 2018 (ADBMS)
PPTX
Sql fundamentals
PPTX
SQL Commands
PPTX
SQL Queries Information
DOCX
Dbms important questions and answers
PDF
SQL Overview
PPTX
PPT
Module02
PPTX
What is SQL Server?
PPTX
Sql basics
Sql Basics And Advanced
Structured query language(sql)ppt
Supporting Transactions
Sql commands
Chapter 1 introduction to sql server
Sql 2009
Introduction to Data Science With R Notes
Sql intro & ddl 1
Islamic University Previous Year Question Solution 2018 (ADBMS)
Sql fundamentals
SQL Commands
SQL Queries Information
Dbms important questions and answers
SQL Overview
Module02
What is SQL Server?
Sql basics
Ad

Similar to SQL (20)

PPTX
Sql 
statements , functions & joins
DOCX
Dbms question
PPTX
Sql statement,functions and joins
PPTX
Fundamentals of Database management system Lab Manual.pptx
PPTX
interenship.pptx
PPTX
SQL data types: INT, VARCHAR, CHAR,.pptx
PPTX
DDL and DML statements.pptx
PPT
SQL200.2 Module 2
PDF
Two dimensional array
DOCX
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
PPTX
ADVANCE ITT BY PRASAD
PPTX
Advanced data structures slide 1 2
PPTX
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PPTX
PPTX
PDF
Micro project project co 3i
PDF
Zoho Interview Questions By Scholarhat.pdf
DOCX
DIRECTIONS READ THE FOLLOWING STUDENT POST AND RESPOND EVALUATE I.docx
Sql 
statements , functions & joins
Dbms question
Sql statement,functions and joins
Fundamentals of Database management system Lab Manual.pptx
interenship.pptx
SQL data types: INT, VARCHAR, CHAR,.pptx
DDL and DML statements.pptx
SQL200.2 Module 2
Two dimensional array
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
ADVANCE ITT BY PRASAD
Advanced data structures slide 1 2
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
Micro project project co 3i
Zoho Interview Questions By Scholarhat.pdf
DIRECTIONS READ THE FOLLOWING STUDENT POST AND RESPOND EVALUATE I.docx
Ad

More from Er. Nawaraj Bhandari (20)

PPTX
Data mining approaches and methods
PPTX
Research trends in data warehousing and data mining
PPTX
Mining Association Rules in Large Database
PPTX
Introduction to data mining and data warehousing
PPTX
Data warehouse testing
PPTX
Data warehouse physical design
PPTX
Data warehouse logical design
PPTX
Classification and prediction in data mining
PPTX
Chapter 3: Simplification of Boolean Function
PPTX
Chapter 6: Sequential Logic
PPTX
Chapter 5: Cominational Logic with MSI and LSI
PPTX
Chapter 4: Combinational Logic
PPTX
Chapter 2: Boolean Algebra and Logic Gates
PPTX
Chapter 1: Binary System
PPTX
Introduction to Electronic Commerce
PPT
Evaluating software development
PPT
Using macros in microsoft excel part 2
PPT
Using macros in microsoft excel part 1
PPTX
Using macros in microsoft access
PPTX
Testing software development
Data mining approaches and methods
Research trends in data warehousing and data mining
Mining Association Rules in Large Database
Introduction to data mining and data warehousing
Data warehouse testing
Data warehouse physical design
Data warehouse logical design
Classification and prediction in data mining
Chapter 3: Simplification of Boolean Function
Chapter 6: Sequential Logic
Chapter 5: Cominational Logic with MSI and LSI
Chapter 4: Combinational Logic
Chapter 2: Boolean Algebra and Logic Gates
Chapter 1: Binary System
Introduction to Electronic Commerce
Evaluating software development
Using macros in microsoft excel part 2
Using macros in microsoft excel part 1
Using macros in microsoft access
Testing software development

Recently uploaded (20)

PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
PPTX
QUANTUM_COMPUTING_AND_ITS_POTENTIAL_APPLICATIONS[2].pptx
PDF
Transcultural that can help you someday.
PPTX
Leprosy and NLEP programme community medicine
PDF
How to run a consulting project- client discovery
PDF
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
PPTX
SAP 2 completion done . PRESENTATION.pptx
PPTX
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
PPTX
Database Infoormation System (DBIS).pptx
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PDF
Data Engineering Interview Questions & Answers Cloud Data Stacks (AWS, Azure,...
PPTX
CYBER SECURITY the Next Warefare Tactics
PDF
Jean-Georges Perrin - Spark in Action, Second Edition (2020, Manning Publicat...
PDF
Microsoft Core Cloud Services powerpoint
PPTX
New ISO 27001_2022 standard and the changes
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PDF
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
PPT
Predictive modeling basics in data cleaning process
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
QUANTUM_COMPUTING_AND_ITS_POTENTIAL_APPLICATIONS[2].pptx
Transcultural that can help you someday.
Leprosy and NLEP programme community medicine
How to run a consulting project- client discovery
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
SAP 2 completion done . PRESENTATION.pptx
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
Database Infoormation System (DBIS).pptx
STERILIZATION AND DISINFECTION-1.ppthhhbx
Data Engineering Interview Questions & Answers Cloud Data Stacks (AWS, Azure,...
CYBER SECURITY the Next Warefare Tactics
Jean-Georges Perrin - Spark in Action, Second Edition (2020, Manning Publicat...
Microsoft Core Cloud Services powerpoint
New ISO 27001_2022 standard and the changes
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
Predictive modeling basics in data cleaning process
Qualitative Qantitative and Mixed Methods.pptx
IBA_Chapter_11_Slides_Final_Accessible.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/