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 Portfolio
newberth
 
Bt0066, database management systems
Bt0066, database management systemsBt0066, database management systems
Bt0066, database management systems
smumbahelp
 
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
ozaixyzo
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SaiMiryala1
 
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
johniemcm5zt
 

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 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
missstevenson01
 
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
missstevenson01
 

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

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

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?