SlideShare a Scribd company logo
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

PPT
Ms Project 2007(advanced)
DOCX
Dbms record
PPT
James Colby Maddox Business Intellignece and Computer Science Portfolio
PPTX
LM7_ Embedded Sql and Dynamic SQL in dbms
PPTX
MRP, MPS, Bill of Material, Numericals
PDF
Exam guide and practice in access
DOCX
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
DOCX
1. What two conditions must be met before an entity can be classif.docx
Ms Project 2007(advanced)
Dbms record
James Colby Maddox Business Intellignece and Computer Science Portfolio
LM7_ Embedded Sql and Dynamic SQL in dbms
MRP, MPS, Bill of Material, Numericals
Exam guide and practice in access
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
1. What two conditions must be met before an entity can be classif.docx

Similar to Sql (20)

PDF
semantics_documentationsbn
PPTX
Database_Systems_Lab_5_Presentation.pptx
PPSX
William Berth Bi Portfolio
PPT
01 basic orders
PPT
Database queries
PPT
3.1- Data Management & Retrieval using data analytics techniques
PPTX
DB2 Sql Query
PPTX
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
DOCX
Bt0066, database management systems
PDF
INT217 Project Report: Excel Dashboard
DOCX
Report-Template.docx
PDF
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
PDF
Microsoft Access 2013 Comprehensive 1st Edition Pratt Solutions Manual
PDF
SAS/Tableau integration
PPTX
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
PPTX
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
PDF
Information Technology P1 Nov 2022 MG Eng.pdf
DOCX
Structured Query Language for Data Management 2 Sructu.docx
PDF
Microsoft Access 2013 Comprehensive 1st Edition Pratt Solutions Manual
semantics_documentationsbn
Database_Systems_Lab_5_Presentation.pptx
William Berth Bi Portfolio
01 basic orders
Database queries
3.1- Data Management & Retrieval using data analytics techniques
DB2 Sql Query
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
Bt0066, database management systems
INT217 Project Report: Excel Dashboard
Report-Template.docx
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
Microsoft Access 2013 Comprehensive 1st Edition Pratt Solutions Manual
SAS/Tableau integration
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
Information Technology P1 Nov 2022 MG Eng.pdf
Structured Query Language for Data Management 2 Sructu.docx
Microsoft Access 2013 Comprehensive 1st Edition Pratt Solutions Manual
Ad

More from missstevenson01 (20)

PPTX
S3 environment
PPTX
The Processor.pptx
PPTX
How Computers Work
PPTX
Lesson 3 - Coding with Minecraft - Variables.pptx
PPTX
Lesson 2 - Coding with Minecraft - Events.pptx
PPTX
Lesson 1 - Coding with Minecraft -Introduction.pptx
PPTX
Lesson2 - Coding with Minecraft - Events.pptx
PPTX
Ethical hacking trojans, worms and spyware
PPTX
Ethical hacking anti virus
PPTX
Ethical hacking introduction to ethical hacking
PPTX
S1 internet safety-chattingonline
PPTX
S3 wireframe diagrams
PPTX
Alien database
PPTX
Video Games and Copyright laws
PPTX
Games Design Document
PPTX
Video game proposal
PPTX
Evaluation
PPTX
H evaluation
PPTX
H testing and debugging
PPTX
H file handling
S3 environment
The Processor.pptx
How Computers Work
Lesson 3 - Coding with Minecraft - Variables.pptx
Lesson 2 - Coding with Minecraft - Events.pptx
Lesson 1 - Coding with Minecraft -Introduction.pptx
Lesson2 - Coding with Minecraft - Events.pptx
Ethical hacking trojans, worms and spyware
Ethical hacking anti virus
Ethical hacking introduction to ethical hacking
S1 internet safety-chattingonline
S3 wireframe diagrams
Alien database
Video Games and Copyright laws
Games Design Document
Video game proposal
Evaluation
H evaluation
H testing and debugging
H file handling
Ad

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
master seminar digital applications in india
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Computing-Curriculum for Schools in Ghana
PDF
Classroom Observation Tools for Teachers
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Lesson notes of climatology university.
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
RMMM.pdf make it easy to upload and study
O5-L3 Freight Transport Ops (International) V1.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
master seminar digital applications in india
TR - Agricultural Crops Production NC III.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Computing-Curriculum for Schools in Ghana
Classroom Observation Tools for Teachers
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Final Presentation General Medicine 03-08-2024.pptx
Pharma ospi slides which help in ospi learning
PPH.pptx obstetrics and gynecology in nursing
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
VCE English Exam - Section C Student Revision Booklet
Lesson notes of climatology university.
2.FourierTransform-ShortQuestionswithAnswers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

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?