SlideShare a Scribd company logo
1 of 44
SQL
National 5 Computing Science
What is SQL?
SQL – Structured Query Language is a language used to manipulate data held within a
database.
Database applications such as Access simply place a graphical user interface
which manipulates and executes various SQL operations.
There are lots of slight variations of SQL syntax but for this course we will be using
MySQL
SQL Operations covered at National 5
We will look at SQL operations which are designed to query, insert and edit data within
a database.
In SQL these operations can be performed by the following commands.
❏SELECT - Returns particular data
❏UPDATE – Updates value/values within a row/rows.
❏INSERT – Inserts new rows (records) into a table.
❏DELETE – Deletes rows from a table.
SELECT Statements
SELECT SQL Statements
SELECT statements are used to retrieve information from a database.
At National 5 to being with our SELECT statements will have 4 main parts
1. The fields you want to display in your results
2. Which tables that the fields are in
3. The criteria for your search
4. Any sort order that you want to apply
Some examples
We will use data from the instructor database as an example
CourseID Title Date Days Capacity Cost
Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
KAY02 Adv. Kayaking 1/9/17 6 8 £450 2
WAL01 Gorge Walking 1/9/17 10 6 £600 3
ABS02 Abseiling
1/10/1
7
2 18 £250 4
MBT01
Mountain
Biking
8/9/17 5 12 £170 2
SELECT QUERY - One Condition
Show the Title, Date, Days, Capacity and Cost of all courses that cost under £500 and
display the results cheapest first.
Step 1 is to SELECT the fields we want:
SELECT Title,Date, Days,Capacity,Cost
SELECT QUERY - One Condition
Show the Title, Date, Days, Capacity and Cost of all courses that cost under £500 and
display the results cheapest first.
Step 2 is to SELECT the table(s) that our fields are from we want:
SELECT Title,Date, Days,Capacity,Cost FROM Course
SELECT QUERY - One Condition
Show the Title, Date, Days, Capacity and Cost of all courses that cost under £500 and
display the results cheapest first.
Step 3 is to specify the criteria we want to use for our query
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500
SELECT QUERY - One Condition
We want to show all courses that cost under £500, we would want to see the Title,Date,
Days, Capacity and Cost.
Step 4 is to specify any sort order
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 ORDER BY Cost
ASC
You can sort in ascending (ASC) or descending (DESC) order
SELECT QUERY - One Condition
1. The fields you want to display in your results
2. Which tables that the fields are in
3. The criteria for your search
4. Any sort order that you want to apply
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 ORDER BY Cost ASC
Fields to be returned
Tables where the fields are
Criteria
Sort Order
SELECT QUERY - One Condition
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 ORDER BY Cost ASC
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
KAY02 Adv. Kayaking 1/9/17 6 8 £450 2
WAL01 Gorge Walking 1/9/17 10 6 £600 3
ABS02 Abseiling 1/10/17 2 18 £250 4
MBT01 Mountain
Biking
1/9/17 5 12 £170 2
SELECT QUERY - One Condition
Only the fields we specified (Title,Date, Days,Capacity,Cost) are returned
Title Date Days Capacity Cost
Mountain
Biking
1/9/17 5 12 £170
Abseiling 1/10/17 2 18 £250
Adv. Kayaking 1/9/17 6 8 £450
SELECT QUERY - More than one condition
We want to show the course details that are under £500 that last 5 days or less and
display them with the longest course first.
Step 1 is to SELECT the fields we want:
SELECT Title,Date, Days,Capacity,Cost
SELECT QUERY - More than one condition
We want to show the course details that are under £500 that last 5 days or less and
display them with the longest course first.
Step 2 is to SELECT the table(s) that our fields are from we want:
SELECT Title,Date, Days,Capacity,Cost FROM Course
SELECT QUERY - More than one condition
We want to show the course details that are under £500 that last 5 days or less and
display them with the longest course first.
Step 3 is to specify the criteria we want to use for our query
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5
SELECT QUERY - More than one condition
We want to show the course details that are under £500 that last 5 days or less and display
them with the longest course first.
Step 4 is to specify any sort order
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5
ORDER BY DAYS DESC
SELECT QUERY - More than one condition
1. The fields you want to display in your results
2. Which tables that the fields are in
3. The criteria for your search
4. Any sort order that you want to apply
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5
ORDER BY DAYS DESC
Fields to be returned Tables where the fields are
Criteria
Sort Order
Search Results
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5
ORDER BY DAYS DESC
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
KAY02 Adv. Kayaking 1/9/17 6 8 £450 2
WAL01 Gorge Walking 1/9/17 10 6 £600 3
ABS02 Abseiling 1/10/17 2 18 £250 4
MBT01 Mountain
Biking
1/9/17 5 12 £170 2
Search Results
Only the fields we specified (Title,Date, Days,Capacity,Cost) are returned
Title Date Days Capacity Cost
Mountain
Biking
1/9/17 5 12 £170
Abseiling 1/10/17 2 18 £250
UPDATE Statements
UPDATE SQL Statements
UPDATE statements are used to amend data currently in the database
An UPDATE statement will have 3 main parts
1. Specify the table that has the information to be updated
2. What the new values are going to be
3. The criteria for which records to update
Update Query - Single Record
A few of the bikes for the BMX Intro course are being repaired and the new capacity for
the course will be 8. We will update the record to show 8 for capacity
Step 1 Specify the table that has the information to be updated
UPDATE Course
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
Needs to be 8
Update Query - Single Record
A few of the bikes for the BMX Intro course are being repaired and the new capacity for
the course will be 8. We will update the record to show 8 for capacity
Step 2 What the new values are going to be
UPDATE Course SET Capacity = 8
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
Update Query - Single Record
A few of the bikes for the BMX Intro course are being repaired and the new capacity for
the course will be 8. We will update the record to show 8 for capacity
Step 3 The criteria for which records to update
UPDATE Course SET Capacity = 8 WHERE CourseID = ‘BMX01’
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
Update Query- Multiple Records
There has been an issue with the training facility and all courses that are due to run on
the 10th of December have had to be changed to the 11th of December
Step 1 Specify the table that has the information to be updated
UPDATE Course
Update Query- Multiple Records
There has been an issue with the training facility and all courses that are due to run on
the 10th of December have had to be changed to the 11th of December
Step 2 What the new values are going to be
UPDATE Course SET Date = ‘2017/12/11’
Dates are in
yyyy/mm/dd format
Update Query- Multiple Records
There has been an issue with the training facility and all courses that are due to run on
the 10th of December have had to be changed to the 11th of December
Step 3 What the new values are going to be
UPDATE Course SET Date = ‘2017/12/11’ WHERE DATE = ‘2017/12/10’
This will change every course that was on the 10th December
Update Query- Partial Fields
There has been a change of staffing and all of the BMX Intro courses will now be taught
by another instructor (ID 3) and will be limited to a capacity of 10.
Step 1 Specify the table that has the information to be updated
UPDATE Course
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
Update Query- Partial Fields
There has been a change of staffing and all of the BMX Intro courses will now be taught
by another instructor (ID 3) and will be a limited to a capacity of 10.
Step 2 What the new values are going to be
UPDATE Course SET Capacity = 10, Instructor ID = 3
Update Query- Multiple Records
There has been a change of staffing and all of the BMX Intro courses will now be taught
by another instructor (ID 3) and will be a limited to a capacity of 10.
Step 3 What the new values are going to be
UPDATE Course SET Capacity = 10, Instructor ID = 3 WHERE Title = ‘BMX Intro’
INSERT Statements
INSERT SQL Statement
INSERT INTO table
VALUES (value1, value2, value3, ...);
The order of the values to be inserted must match the column order
An alternative method allows you to specify the order of the fields and their data values
in the format below:
INSERT INTO table (column3, column1, column2, ...)
VALUES (value3, value1, value2, ...);
INSERT Query - Data for every field
A new instructor joins and his details need to be added. He will have an ID of 5 and his
name is D Thomas, he was born on the 1/5/86 and is a Grade 5
Step 1 is to specify which table to insert the data
INSERT INTO Instructor
Instructor
ID
Name DOB Grade
5 D Thomas 1/5/86 5
INSERT Query - Data for every field
A new instructor joins and their details need to be added. They will have an ID of 5 and
their name is D Thomas, they were born on the 1/5/86 and they are a Grade 5
Step 2 is to specify the values to be inserted
INSERT INTO Instructor VALUES (5,‘D Thomas’, ‘1985/05/01’,5)
Instructor
ID
Name DOB Grade
5 D Thomas 1/5/86 5
Dates are in
yyyy/mm/dd format
String values are surrounded by an ‘
Partial INSERT Query
The organisation is running a new BMX Course, it’s course ID is BMX05 and will be a BMX
Advanced, it will run for 4 days and can take 10 people. It will be ran by Instructor R Hay
( ID 3) and will cost £400. They haven’t decided a date yet.
Step 1 is to specify which table to insert the data
INSERT INTO Course
CourseID Title Date Days Capacity Cost Instructor
ID
BMX05 BMX Advanced 4 10 £400 3
Partial INSERT Query
The organisation is running a new BMX Course, it’s course ID is BMX05 and will be a BMX
Advanced, it will run for 4 days and can take 10 people. It will be ran by Instructor R Hay
( ID 3) and will cost £400. They haven’t decided a date yet.
Step 2 is to specify which fields you are entering data for
INSERT INTO Course (CourseID,Title,Days,Capacity,Cost,InstructorID)
CourseID Title Date Days Capacity Cost Instructor
ID
BMX05 BMX Advanced 4 10 £400 3
Partial INSERT Query
The organisation is running a new BMX Course, it’s course ID is BMX05 and will be a BMX
Advanced, it will run for 4 days and can take 10 people. It will be ran by Instructor R Hay
( ID 3) and will cost £400. They haven’t decided a date yet.
Step 3 is to specify the values for each field
INSERT INTO Course (CourseID,Title,Days,Capacity,Cost,InstructorID) VALUES
(‘BMX05’,‘BMX Advanced’,4,10,400,3)
DELETE Statements
DELETE SQL Statements
DELETE statements are used to delete data currently in the database
A DELETE statement has 2 main parts
1. Specify the table that has the information to be deleted
2. The criteria for which records have to be deleted
DELETE SQL Statement
DELETE FROM tbl_name
[WHERE where_condition]
The DELETE statement deletes rows from the table and returns the number of
deleted rows.
The conditions in the WHERE clause identify which rows to delete.
If there is no WHERE clause, all rows in the table(s) are deleted.
DELETE Query - Single Record
The BMX Advanced course on the 4th May is now cancelled so should be deleted
Step 1 Specify the table that has the information to be deleted
DELETE FROM Course
CourseID Title Date Days Capacity Cost Instructor
ID
BMX05 BMX Advanced 2017/5/4 4 10 £400 3
DELETE Query - Single Record
The BMX Advanced course on the 4th May is now cancelled so should be deleted
Step 2 The criteria for which records have to be deleted
DELETE FROM Course WHERE CourseID = ‘BMX05’
CourseID Title Date Days Capacity Cost Instructor
ID
BMX05 BMX Advanced 2017/5/4 4 10 £400 3
Why did we use the
CourseID and not the Title?

More Related Content

Similar to Sql

William Berth Bi Portfolio
William Berth Bi PortfolioWilliam Berth Bi Portfolio
William Berth Bi Portfolionewberth
 
Database queries
Database queriesDatabase queries
Database querieslaiba29012
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxBhupendraShahi6
 
Bt0066, database management systems
Bt0066, database management systemsBt0066, database management systems
Bt0066, database management systemssmumbahelp
 
INT217 Project Report: Excel Dashboard
INT217 Project Report: Excel DashboardINT217 Project Report: Excel Dashboard
INT217 Project Report: Excel DashboardQazi Maaz Arshad
 
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdfCC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdfozaixyzo
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSabrinaShanta2
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSaiMiryala1
 
Structured Query Language for Data Management 2 Sructu.docx
Structured Query Language for Data Management      2 Sructu.docxStructured Query Language for Data Management      2 Sructu.docx
Structured Query Language for Data Management 2 Sructu.docxjohniemcm5zt
 
2010 Singapore Microsoft Office Academic Skills Challenge - Briefing Sdides
2010 Singapore Microsoft Office Academic Skills Challenge - Briefing Sdides2010 Singapore Microsoft Office Academic Skills Challenge - Briefing Sdides
2010 Singapore Microsoft Office Academic Skills Challenge - Briefing Sdidesmoascsingapore
 
SAP Variant configuration
SAP Variant configurationSAP Variant configuration
SAP Variant configurationKumbum Ramesh
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)Ehtisham Ali
 
Abhi rana)factor rating method of plant location
Abhi rana)factor rating method of plant locationAbhi rana)factor rating method of plant location
Abhi rana)factor rating method of plant locationAbhishek Rana
 

Similar to Sql (20)

semantics_documentationsbn
semantics_documentationsbnsemantics_documentationsbn
semantics_documentationsbn
 
SQL
SQLSQL
SQL
 
William Berth Bi Portfolio
William Berth Bi PortfolioWilliam Berth Bi Portfolio
William Berth Bi Portfolio
 
01 basic orders
01   basic orders01   basic orders
01 basic orders
 
Database queries
Database queriesDatabase queries
Database queries
 
DB2 Sql Query
DB2 Sql QueryDB2 Sql Query
DB2 Sql Query
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
Bt0066, database management systems
Bt0066, database management systemsBt0066, database management systems
Bt0066, database management systems
 
INT217 Project Report: Excel Dashboard
INT217 Project Report: Excel DashboardINT217 Project Report: Excel Dashboard
INT217 Project Report: Excel Dashboard
 
Report-Template.docx
Report-Template.docxReport-Template.docx
Report-Template.docx
 
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdfCC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
 
SAS/Tableau integration
SAS/Tableau integrationSAS/Tableau integration
SAS/Tableau integration
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
Structured Query Language for Data Management 2 Sructu.docx
Structured Query Language for Data Management      2 Sructu.docxStructured Query Language for Data Management      2 Sructu.docx
Structured Query Language for Data Management 2 Sructu.docx
 
2010 Singapore Microsoft Office Academic Skills Challenge - Briefing Sdides
2010 Singapore Microsoft Office Academic Skills Challenge - Briefing Sdides2010 Singapore Microsoft Office Academic Skills Challenge - Briefing Sdides
2010 Singapore Microsoft Office Academic Skills Challenge - Briefing Sdides
 
SAP Variant configuration
SAP Variant configurationSAP Variant configuration
SAP Variant configuration
 
My Sql
My Sql My Sql
My Sql
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)
 
Abhi rana)factor rating method of plant location
Abhi rana)factor rating method of plant locationAbhi rana)factor rating method of plant location
Abhi rana)factor rating method of plant location
 

More from missstevenson01

Lesson 3 - Coding with Minecraft - Variables.pptx
Lesson 3 -  Coding with Minecraft -  Variables.pptxLesson 3 -  Coding with Minecraft -  Variables.pptx
Lesson 3 - Coding with Minecraft - Variables.pptxmissstevenson01
 
Lesson 2 - Coding with Minecraft - Events.pptx
Lesson 2 - Coding with Minecraft - Events.pptxLesson 2 - Coding with Minecraft - Events.pptx
Lesson 2 - Coding with Minecraft - Events.pptxmissstevenson01
 
Lesson 1 - Coding with Minecraft -Introduction.pptx
Lesson 1 - Coding with Minecraft -Introduction.pptxLesson 1 - Coding with Minecraft -Introduction.pptx
Lesson 1 - Coding with Minecraft -Introduction.pptxmissstevenson01
 
Lesson2 - Coding with Minecraft - Events.pptx
Lesson2 - Coding with Minecraft - Events.pptxLesson2 - Coding with Minecraft - Events.pptx
Lesson2 - Coding with Minecraft - Events.pptxmissstevenson01
 
Ethical hacking trojans, worms and spyware
Ethical hacking    trojans, worms and spywareEthical hacking    trojans, worms and spyware
Ethical hacking trojans, worms and spywaremissstevenson01
 
Ethical hacking anti virus
Ethical hacking   anti virusEthical hacking   anti virus
Ethical hacking anti virusmissstevenson01
 
Ethical hacking introduction to ethical hacking
Ethical hacking   introduction to ethical hackingEthical hacking   introduction to ethical hacking
Ethical hacking introduction to ethical hackingmissstevenson01
 
S1 internet safety-chattingonline
S1 internet safety-chattingonlineS1 internet safety-chattingonline
S1 internet safety-chattingonlinemissstevenson01
 
Video Games and Copyright laws
Video Games and Copyright lawsVideo Games and Copyright laws
Video Games and Copyright lawsmissstevenson01
 

More from missstevenson01 (20)

S3 environment
S3 environmentS3 environment
S3 environment
 
The Processor.pptx
The Processor.pptxThe Processor.pptx
The Processor.pptx
 
How Computers Work
How Computers WorkHow Computers Work
How Computers Work
 
Lesson 3 - Coding with Minecraft - Variables.pptx
Lesson 3 -  Coding with Minecraft -  Variables.pptxLesson 3 -  Coding with Minecraft -  Variables.pptx
Lesson 3 - Coding with Minecraft - Variables.pptx
 
Lesson 2 - Coding with Minecraft - Events.pptx
Lesson 2 - Coding with Minecraft - Events.pptxLesson 2 - Coding with Minecraft - Events.pptx
Lesson 2 - Coding with Minecraft - Events.pptx
 
Lesson 1 - Coding with Minecraft -Introduction.pptx
Lesson 1 - Coding with Minecraft -Introduction.pptxLesson 1 - Coding with Minecraft -Introduction.pptx
Lesson 1 - Coding with Minecraft -Introduction.pptx
 
Lesson2 - Coding with Minecraft - Events.pptx
Lesson2 - Coding with Minecraft - Events.pptxLesson2 - Coding with Minecraft - Events.pptx
Lesson2 - Coding with Minecraft - Events.pptx
 
Ethical hacking trojans, worms and spyware
Ethical hacking    trojans, worms and spywareEthical hacking    trojans, worms and spyware
Ethical hacking trojans, worms and spyware
 
Ethical hacking anti virus
Ethical hacking   anti virusEthical hacking   anti virus
Ethical hacking anti virus
 
Ethical hacking introduction to ethical hacking
Ethical hacking   introduction to ethical hackingEthical hacking   introduction to ethical hacking
Ethical hacking introduction to ethical hacking
 
S1 internet safety-chattingonline
S1 internet safety-chattingonlineS1 internet safety-chattingonline
S1 internet safety-chattingonline
 
S3 wireframe diagrams
S3 wireframe diagramsS3 wireframe diagrams
S3 wireframe diagrams
 
Alien database
Alien databaseAlien database
Alien database
 
Video Games and Copyright laws
Video Games and Copyright lawsVideo Games and Copyright laws
Video Games and Copyright laws
 
Games Design Document
Games Design DocumentGames Design Document
Games Design Document
 
Video game proposal
Video game proposalVideo game proposal
Video game proposal
 
Evaluation
EvaluationEvaluation
Evaluation
 
H evaluation
H evaluationH evaluation
H evaluation
 
H testing and debugging
H testing and debuggingH testing and debugging
H testing and debugging
 
H file handling
H file handlingH file handling
H file handling
 

Recently uploaded

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 

Recently uploaded (20)

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 

Sql

  • 2. What is SQL? SQL – Structured Query Language is a language used to manipulate data held within a database. Database applications such as Access simply place a graphical user interface which manipulates and executes various SQL operations. There are lots of slight variations of SQL syntax but for this course we will be using MySQL
  • 3. SQL Operations covered at National 5 We will look at SQL operations which are designed to query, insert and edit data within a database. In SQL these operations can be performed by the following commands. ❏SELECT - Returns particular data ❏UPDATE – Updates value/values within a row/rows. ❏INSERT – Inserts new rows (records) into a table. ❏DELETE – Deletes rows from a table.
  • 5. SELECT SQL Statements SELECT statements are used to retrieve information from a database. At National 5 to being with our SELECT statements will have 4 main parts 1. The fields you want to display in your results 2. Which tables that the fields are in 3. The criteria for your search 4. Any sort order that you want to apply
  • 6. Some examples We will use data from the instructor database as an example CourseID Title Date Days Capacity Cost Instructor ID BMX01 BMX Intro 1/9/17 3 12 £500 1 KAY02 Adv. Kayaking 1/9/17 6 8 £450 2 WAL01 Gorge Walking 1/9/17 10 6 £600 3 ABS02 Abseiling 1/10/1 7 2 18 £250 4 MBT01 Mountain Biking 8/9/17 5 12 £170 2
  • 7. SELECT QUERY - One Condition Show the Title, Date, Days, Capacity and Cost of all courses that cost under £500 and display the results cheapest first. Step 1 is to SELECT the fields we want: SELECT Title,Date, Days,Capacity,Cost
  • 8. SELECT QUERY - One Condition Show the Title, Date, Days, Capacity and Cost of all courses that cost under £500 and display the results cheapest first. Step 2 is to SELECT the table(s) that our fields are from we want: SELECT Title,Date, Days,Capacity,Cost FROM Course
  • 9. SELECT QUERY - One Condition Show the Title, Date, Days, Capacity and Cost of all courses that cost under £500 and display the results cheapest first. Step 3 is to specify the criteria we want to use for our query SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500
  • 10. SELECT QUERY - One Condition We want to show all courses that cost under £500, we would want to see the Title,Date, Days, Capacity and Cost. Step 4 is to specify any sort order SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 ORDER BY Cost ASC You can sort in ascending (ASC) or descending (DESC) order
  • 11. SELECT QUERY - One Condition 1. The fields you want to display in your results 2. Which tables that the fields are in 3. The criteria for your search 4. Any sort order that you want to apply SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 ORDER BY Cost ASC Fields to be returned Tables where the fields are Criteria Sort Order
  • 12. SELECT QUERY - One Condition SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 ORDER BY Cost ASC CourseID Title Date Days Capacity Cost Instructor ID BMX01 BMX Intro 1/9/17 3 12 £500 1 KAY02 Adv. Kayaking 1/9/17 6 8 £450 2 WAL01 Gorge Walking 1/9/17 10 6 £600 3 ABS02 Abseiling 1/10/17 2 18 £250 4 MBT01 Mountain Biking 1/9/17 5 12 £170 2
  • 13. SELECT QUERY - One Condition Only the fields we specified (Title,Date, Days,Capacity,Cost) are returned Title Date Days Capacity Cost Mountain Biking 1/9/17 5 12 £170 Abseiling 1/10/17 2 18 £250 Adv. Kayaking 1/9/17 6 8 £450
  • 14. SELECT QUERY - More than one condition We want to show the course details that are under £500 that last 5 days or less and display them with the longest course first. Step 1 is to SELECT the fields we want: SELECT Title,Date, Days,Capacity,Cost
  • 15. SELECT QUERY - More than one condition We want to show the course details that are under £500 that last 5 days or less and display them with the longest course first. Step 2 is to SELECT the table(s) that our fields are from we want: SELECT Title,Date, Days,Capacity,Cost FROM Course
  • 16. SELECT QUERY - More than one condition We want to show the course details that are under £500 that last 5 days or less and display them with the longest course first. Step 3 is to specify the criteria we want to use for our query SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5
  • 17. SELECT QUERY - More than one condition We want to show the course details that are under £500 that last 5 days or less and display them with the longest course first. Step 4 is to specify any sort order SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5 ORDER BY DAYS DESC
  • 18. SELECT QUERY - More than one condition 1. The fields you want to display in your results 2. Which tables that the fields are in 3. The criteria for your search 4. Any sort order that you want to apply SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5 ORDER BY DAYS DESC Fields to be returned Tables where the fields are Criteria Sort Order
  • 19. Search Results SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5 ORDER BY DAYS DESC CourseID Title Date Days Capacity Cost Instructor ID BMX01 BMX Intro 1/9/17 3 12 £500 1 KAY02 Adv. Kayaking 1/9/17 6 8 £450 2 WAL01 Gorge Walking 1/9/17 10 6 £600 3 ABS02 Abseiling 1/10/17 2 18 £250 4 MBT01 Mountain Biking 1/9/17 5 12 £170 2
  • 20. Search Results Only the fields we specified (Title,Date, Days,Capacity,Cost) are returned Title Date Days Capacity Cost Mountain Biking 1/9/17 5 12 £170 Abseiling 1/10/17 2 18 £250
  • 22. UPDATE SQL Statements UPDATE statements are used to amend data currently in the database An UPDATE statement will have 3 main parts 1. Specify the table that has the information to be updated 2. What the new values are going to be 3. The criteria for which records to update
  • 23. Update Query - Single Record A few of the bikes for the BMX Intro course are being repaired and the new capacity for the course will be 8. We will update the record to show 8 for capacity Step 1 Specify the table that has the information to be updated UPDATE Course CourseID Title Date Days Capacity Cost Instructor ID BMX01 BMX Intro 1/9/17 3 12 £500 1 Needs to be 8
  • 24. Update Query - Single Record A few of the bikes for the BMX Intro course are being repaired and the new capacity for the course will be 8. We will update the record to show 8 for capacity Step 2 What the new values are going to be UPDATE Course SET Capacity = 8 CourseID Title Date Days Capacity Cost Instructor ID BMX01 BMX Intro 1/9/17 3 12 £500 1
  • 25. Update Query - Single Record A few of the bikes for the BMX Intro course are being repaired and the new capacity for the course will be 8. We will update the record to show 8 for capacity Step 3 The criteria for which records to update UPDATE Course SET Capacity = 8 WHERE CourseID = ‘BMX01’ CourseID Title Date Days Capacity Cost Instructor ID BMX01 BMX Intro 1/9/17 3 12 £500 1
  • 26. Update Query- Multiple Records There has been an issue with the training facility and all courses that are due to run on the 10th of December have had to be changed to the 11th of December Step 1 Specify the table that has the information to be updated UPDATE Course
  • 27. Update Query- Multiple Records There has been an issue with the training facility and all courses that are due to run on the 10th of December have had to be changed to the 11th of December Step 2 What the new values are going to be UPDATE Course SET Date = ‘2017/12/11’ Dates are in yyyy/mm/dd format
  • 28. Update Query- Multiple Records There has been an issue with the training facility and all courses that are due to run on the 10th of December have had to be changed to the 11th of December Step 3 What the new values are going to be UPDATE Course SET Date = ‘2017/12/11’ WHERE DATE = ‘2017/12/10’ This will change every course that was on the 10th December
  • 29. Update Query- Partial Fields There has been a change of staffing and all of the BMX Intro courses will now be taught by another instructor (ID 3) and will be limited to a capacity of 10. Step 1 Specify the table that has the information to be updated UPDATE Course CourseID Title Date Days Capacity Cost Instructor ID BMX01 BMX Intro 1/9/17 3 12 £500 1
  • 30. Update Query- Partial Fields There has been a change of staffing and all of the BMX Intro courses will now be taught by another instructor (ID 3) and will be a limited to a capacity of 10. Step 2 What the new values are going to be UPDATE Course SET Capacity = 10, Instructor ID = 3
  • 31. Update Query- Multiple Records There has been a change of staffing and all of the BMX Intro courses will now be taught by another instructor (ID 3) and will be a limited to a capacity of 10. Step 3 What the new values are going to be UPDATE Course SET Capacity = 10, Instructor ID = 3 WHERE Title = ‘BMX Intro’
  • 33. INSERT SQL Statement INSERT INTO table VALUES (value1, value2, value3, ...); The order of the values to be inserted must match the column order An alternative method allows you to specify the order of the fields and their data values in the format below: INSERT INTO table (column3, column1, column2, ...) VALUES (value3, value1, value2, ...);
  • 34. INSERT Query - Data for every field A new instructor joins and his details need to be added. He will have an ID of 5 and his name is D Thomas, he was born on the 1/5/86 and is a Grade 5 Step 1 is to specify which table to insert the data INSERT INTO Instructor Instructor ID Name DOB Grade 5 D Thomas 1/5/86 5
  • 35. INSERT Query - Data for every field A new instructor joins and their details need to be added. They will have an ID of 5 and their name is D Thomas, they were born on the 1/5/86 and they are a Grade 5 Step 2 is to specify the values to be inserted INSERT INTO Instructor VALUES (5,‘D Thomas’, ‘1985/05/01’,5) Instructor ID Name DOB Grade 5 D Thomas 1/5/86 5 Dates are in yyyy/mm/dd format String values are surrounded by an ‘
  • 36. Partial INSERT Query The organisation is running a new BMX Course, it’s course ID is BMX05 and will be a BMX Advanced, it will run for 4 days and can take 10 people. It will be ran by Instructor R Hay ( ID 3) and will cost £400. They haven’t decided a date yet. Step 1 is to specify which table to insert the data INSERT INTO Course CourseID Title Date Days Capacity Cost Instructor ID BMX05 BMX Advanced 4 10 £400 3
  • 37. Partial INSERT Query The organisation is running a new BMX Course, it’s course ID is BMX05 and will be a BMX Advanced, it will run for 4 days and can take 10 people. It will be ran by Instructor R Hay ( ID 3) and will cost £400. They haven’t decided a date yet. Step 2 is to specify which fields you are entering data for INSERT INTO Course (CourseID,Title,Days,Capacity,Cost,InstructorID) CourseID Title Date Days Capacity Cost Instructor ID BMX05 BMX Advanced 4 10 £400 3
  • 38. Partial INSERT Query The organisation is running a new BMX Course, it’s course ID is BMX05 and will be a BMX Advanced, it will run for 4 days and can take 10 people. It will be ran by Instructor R Hay ( ID 3) and will cost £400. They haven’t decided a date yet. Step 3 is to specify the values for each field INSERT INTO Course (CourseID,Title,Days,Capacity,Cost,InstructorID) VALUES (‘BMX05’,‘BMX Advanced’,4,10,400,3)
  • 40. DELETE SQL Statements DELETE statements are used to delete data currently in the database A DELETE statement has 2 main parts 1. Specify the table that has the information to be deleted 2. The criteria for which records have to be deleted
  • 41. DELETE SQL Statement DELETE FROM tbl_name [WHERE where_condition] The DELETE statement deletes rows from the table and returns the number of deleted rows. The conditions in the WHERE clause identify which rows to delete. If there is no WHERE clause, all rows in the table(s) are deleted.
  • 42. DELETE Query - Single Record The BMX Advanced course on the 4th May is now cancelled so should be deleted Step 1 Specify the table that has the information to be deleted DELETE FROM Course CourseID Title Date Days Capacity Cost Instructor ID BMX05 BMX Advanced 2017/5/4 4 10 £400 3
  • 43. DELETE Query - Single Record The BMX Advanced course on the 4th May is now cancelled so should be deleted Step 2 The criteria for which records have to be deleted DELETE FROM Course WHERE CourseID = ‘BMX05’ CourseID Title Date Days Capacity Cost Instructor ID BMX05 BMX Advanced 2017/5/4 4 10 £400 3
  • 44. Why did we use the CourseID and not the Title?