N5 SQL Cheat Sheet
SELECT Statements INSERT Statements - Entering every field in a record Order of the fields when
SELECT statements are used to query a database and return some values. inserting is important!
INSERT Statements add new records into the database
Specify the fields you want to display: SELECT fieldname1,fieldname2 Specify the tablename to insert into: INSERT INTO Instructor
Name the tables that the fields are in: FROM tablename List the values to be inserted: VALUES(5,’D Thomas’,’1985-11-30’,5)
The criteria for your search: WHERE fieldname1 = ‘value’
Any sort order: ORDER BY fieldname1 ASC/DESC, fieldname2 ASC/DESC ; Example:
INSERT INTO Instructor VALUES (5,‘D Thomas’, ‘1985-11-30’,5);
Example: Example:
SELECT name,age SELECT name,age
INSERT Statements - Only entering values for some fields
FROM pupil WHERE age<18 FROM pupil WHERE age<18 SQL Dates are stored in
Sometimes you may not be adding a value for every single field the format yyyy-mm-dd
ORDER BY name ASC; ORDER BY name DESC, age ASC;
Specify the tablename to insert into: INSERT INTO Instructor
When searching for text values you must use ‘speech marksʻ Specify the fields you are entering data for: (InstructorID,Name)
You can use logical conditions such as AND, OR and NOT to create more complex queries
List the values to be inserted: (5,’D Thomas’)
Example:
DELETE Statements INSERT INTO Instructor(InstructorID,Name) VALUES (5,‘D Thomas’);
DELETE statements delete records from a table
Specify that you are carrying out a delete operation: DELETE
UPDATE Statements
Which tables that the fields are in: FROM tablename
UPDATE statements will edit existing data in the table
The criteria for your search: WHERE fieldname1 = “value” ;
Name the table where the records are: UPDATE tablename
Example: Set the new values: SET fieldname = value1,fieldname = value2
DELETE FROM Course WHERE CourseID = ‘BMX05’; Criteria if there are any: WHERE fieldname = fieldvalue ;
Example:
If you don't specify criteria then every record in the table will be deleted UPDATE Course SET Date = ‘2017-12-11’ WHERE DATE = ‘2017-12-10’;
EQUI Joins
An equi join is when two matching fields from related tables are selected (joined) using a key field that is present in both tables.
Example: Equi Join
SELECT Instructor.Name, Course.Title, Course.Date FROM Instructor,Course WHERE Course.InstructorID = Instructor.InstructorID ;