Structured query language (SQL) Flashcards
What is SQL used for?
creating, maintaining and accessing databases
it allows users to search for records that contain a particular item or items of data
data
units of information
database
data store designed in an organised way, making it easier to search for the information you need
records
all of the data related to one entity in a database
What is data in a database stored as?
records
What are records stored in?
files
attribute
a single database component
List 6 SQL commands
SELECT FROM WHERE LIKE AND OR
How are databases formatted/what do they look like?
a table, for example:
StudentNo FirstName Surname DOB TutorGroup
001123 Fred Smith 28/01 10X
001142 Angela Smith 13/10 10Y
002567 Mary Green 15/2 10X
WHERE keyword
used for searching
e.g.
SELECT FirstName, Surname (the fields to display)
FROM Students (the name of the database)
WHERE TutorGroup = “10X” AND DOB > 31/1/2001; (the search criteria)
formatting an SQL command (5)
- write commands on separate lines to make them easier to understand
- every command (not line) ends with a ;
- commands are not case sensitive
- fields are separated with a comma
- the fields may also be shown in quotations
SELECT keyword
used to display records
e.g.
SELECT * FROM Students; (Displays all the records and shows all the fields, the * symbol means ‘show all fields’)
SELECT FirstName, Surname FROM Students;
wildcards
- the % symbol is used as a substitute for one or many characters
- the * symbol is used to represent ‘all fields’
WHERE TutorGroup LIKE “%X”;
…returns all fields with a tutor group ending in an X, and any characters before that
LIKE command
finds matches for an incomplete word:
SELECT * FROM Students WHERE TutorGroup LIKE “%X”;
the = command can be used to find complete words
AND and OR commands (Boolean operators)
can also be used to retrieve data
FROM command
specifies the name of the database to search
Details of the employees of a multinational company are stored in a database table named ‘Employees’ using these fields:
- EmployeeNo
- FirstName
- Surname
- DOB
- Country
- JobFunction
Write the SQL commands that would return the following records:
a) All employees showing only the Surname and Country fields. [2]
b) Employees with the word ‘computer’ in their job titles. All fields should be displayed. [2]
a) SELECT Surname, Country FROM Employees;
b) SELECT * FROM Employees
WHERE JobFunction LIKE “%computer%”;
Details of a school’s students are stored in a database named ‘Students’ containing the following fields:
- FirstName
- Surname
- DOB
- Gender
- Address1
- Address2
- Town
- PostCode
- SchoolYear
- TutorGroup
Write the SQL commands that would return the following records:
a) all students showing the FirstName, Surname and PostCode fields
b) Students who are in the year 10 tutor group named East showing all the fields. [5]
a) SELECT FirstName, Surname, Postcode FROM Students;
b) SELECT * FROM Students
WHERE TutorGroup = “EAST” AND SchoolYear = 10;