Structured Query Language (SQL) A Level Only Photocopiable/digital resources may only be copied by the purchasing institution on a single site and for their own use © ZigZag Education, 2016
Structured Query Language Structured query language (SQL) is the language used to search databases. Here is an example of an SQL statement. SELECT FirstName FROM Student WHERE Gender = “Male” Fields to show The table Search criteria This statement selects the FirstName field from the Student table and only shows the male students. Consider this table: StudentID FirstName LastName Address Gender 1 John Curtis 12 Brook Lane Male 2 Ben Jackson 1 Totters Lane 3 Sarah Smith 60 Belsize Rd Female © ZigZag Education, 2016
Results SELECT FirstName FROM Student WHERE Gender = “Male” StudentID FirstName LastName Address Gender 1 John Curtis 12 Brook Lane Male 2 Ben Jackson 1 Totters Lane 3 Sarah Smith 60 Belsize Rd Female SELECT FirstName FROM Student WHERE Gender = “Male” Fields to show The table Search criteria These are the results of the query. FirstName John Ben © ZigZag Education, 2016
Comparison Operators The following comparison operators are used in SQL statements: Comparison operator Symbol Equal to = Less than < Greater than > Less than or equal to <= Greater than or equal to >= Not equal to <> © ZigZag Education, 2016
Combining Conditions SELECT FirstName FROM Student StudentID FirstName LastName TestScore Gender 1 John Curtis 55 Male 2 Ben Jackson 75 3 Sarah Smith 80 Female We can use the AND operator to combine conditions. SELECT FirstName FROM Student WHERE Gender = “Male” AND TestScore >= 75 These are the results of the query. TestScore Ben © ZigZag Education, 2016
Inserting a Record A new record can be added to a table using the INSERT command. INSERT INTO Student VALUES (4, “Eloise”, “Roberts”, “9 Baynes Mews”, “Female”) StudentID FirstName LastName Address Gender 1 John Curtis 12 Brook Lane Male 2 Ben Jackson 1 Totters Lane 3 Sarah Smith 60 Belsize Rd Female 4 Eloise Roberts 9 Baynes Mews © ZigZag Education, 2016
Deleting a Record DELETE FROM Student WHERE StudentID = 3 Records can be deleted from a table using the DELETE command. DELETE FROM Student WHERE StudentID = 3 StudentID FirstName LastName Address Gender 1 John Curtis 12 Brook Lane Male 2 Ben Jackson 1 Totters Lane 3 Sarah Smith 60 Belsize Rd Female 4 Eloise Roberts 9 Baynes Mews StudentID FirstName LastName Address Gender 1 John Curtis 12 Brook Lane Male 2 Ben Jackson 1 Totters Lane 4 Eloise Roberts 9 Baynes Mews Female © ZigZag Education, 2016
Updating a Record UPDATE Student SET Address = “45 Cleveland Rd” Records can be updated using the UPDATE command. UPDATE Student SET Address = “45 Cleveland Rd” WHERE StudentID = 2 StudentID FirstName LastName Address Gender 1 John Curtis 12 Brook Lane Male 2 Ben Jackson 1 Totters Lane 4 Eloise Roberts 9 Baynes Mews Female 45 Cleveland Rd © ZigZag Education, 2016
Multiple Tables SELECT FirstName, LastName, Title, TeacherName Data can be retrieved from multiple tables using an SQL statement. SELECT FirstName, LastName, Title, TeacherName FROM Student, Teacher WHERE Student.TeacherID = Teacher.TeacherID StudentID FirstName LastName TeacherID 001 Alex Bennett 002 James Hadwen 003 Eloise Roberts 004 Patrick Dua-Brown TeacherID Title TeacherName 001 Mr Smith 002 Ford 003 Mrs Patel 004 Miss Wright A linking condition is used to join the two tables together. Dot notation is used to specify which table a field is in. © ZigZag Education, 2016
Sorting Results SELECT FirstName FROM Student WHERE Gender = “Male” The results of a query can be sorted using the ORDER BY command. SELECT FirstName FROM Student WHERE Gender = “Male” ORDER BY LastName This will order the results by the students’ last names in ascending order. © ZigZag Education, 2016
Creating a Table CREATE TABLE Student ( A new table can be created using the CREATE TABLE command. CREATE TABLE Student ( StudentID INT PRIMARY KEY NOT NULL FirstName VARCHAR(20) LastName VARCHAR(20) Gender VARCHAR(1) ) NOT NULL means the field can’t be left blank. VARCHAR is a string with a variable length; the maximum length is the value in the brackets. © ZigZag Education, 2016
END