Computing
Computer Science LearnITWithMrC ⛯ Year 7 Year 8 Year 9 GCSE
Responsive image

Lesson Video

77

Lesson Tasks

  • Watch the Lesson video
    Make notes if needed.
  • Open your Learning Journal
    Complete Task 1 in your Learning Journal
  • Complete the learning activities
    Make sure you complete the book tasks in your Unit Booklet
  • Complete End of Task Assessment
    Update your learning objectives

What do I need to Learn?

0 results forGuest
I need to learn how to use SQL to retrieve data from a relational database, using the commands: SELECT FROM, WHERE, ORDER BY, ASC / DESC
I need to learn how to use SQL to insert data into a relational database using the commands. INSERT INTO, VALUES
I need to learn how to use SQL to update data in a relational database using the commands. UPDATE, SET, WHERE
I need to learn how to use SQL to delete data in a relational database using the commands. DELETE FROM, WHERE

Key Terms

SELECT INSERT INTO UPDATE condition query ORDER BY DELETE

Task 0 - Getting organised Click to see more


Task: Learning Journal

Open your Learning Journal by clicking on the image below

Good notes will help you organise and process data and information


Task 1 - Retrieving data using SQL Click to see more

1 Using SQL to Query a Relational Database

Structured Query Language (SQL) is a language that was developed as a method for creating, selecting, inserting, editing(updating) and deleting tables, records and fields within a relational database.

Using SQL we can perform various operations on data in out database tables.

We can, for instance, SELECT, INSERT, DELETE and UPDATE tables, records and fields.

  • SQL is a standardized query language for requesting information from a database.
  • Query is another word for questioning the Database using search criteria.

    The most commonly used SQL statements include:

    • SELECT
      • FROM
      • WHERE
      • ORDER BY
    • INSERT
    • UPDATE
    • DELETE

    Task 2 - SELECT Click to see more

    2 - SQL Syntax - SELECT

    SELECT .. FROM

    • The SELECT statement is used to retrieve(Extract) a collection of fields from one or more tables in a database.
    • The syntax is:
    • SELECT will retrieve data(List of Fields) from one or more tables.
    • SELECT has optional keywords such as:
      • FROM to list the table(s) where the data is to be retrieved from.
      • WHERE to allow selection of data using search criteria.
      • ORDER BY lists the column(fields) that the results are to be sorted on, either Ascending or Descending(default is Ascending).
    • The asterisk (*) is also important in writing SQL statements, indicating that all columns(Fields) should be selected.

    Retrieving Data from a Relational Database

    Simple: SQL- SELECT

    • Lets do some SQL by working through the following SQL Select Tutorial using theW3 Schools SQL link below :

    SELECT .. FROM .. WHERE

    • EXAMPLE 1: Let's look at a Customer table example.
    *CustID Title Initial Surname Address City Phone DoB
    C001 Mrs C Flowers 23 Rose Ave Bourne 01778 123456 01/11/69
    C002 Mr J Eames 100 Dowe Rd Bourne 01778 654321 23/09/87
    C003 Mr H Gull 34 High St Bourne 01778 657231 30/07/99
    C004 Ms P Jones 89 Knight St Bourne 01778 856021 10/10/56
    • A valid SQL statement to select All People in the Customer table who was born between 01/01/1960 and 01/01/2000 and displaying the results in descending order of Surname would be:
    SELECT *
           FROM Customer
           WHERE DoB BETWEEN #01/01/1960# AND #01/01/2000#
           ORDER BY Surname DESC;
    
    • This SQL query will return the following records:

    3.7.2_DB_SQL1.png

    • EXAMPLE 2: In this example we will use the Books table to select BookID, Title, Genre, Rating and PublishDate with a rating of 5 or above or with a publish date after 1970 and displaying the results in ascending order of Title would be:
    *BookID Title Author Genre PublishDate Rating
    B123 Charlotte's Web E. B. White Fiction 01/01/1952 5
    B456 David Copperfield Charles Dickens Fiction 10/12/1850 5
    B789 How To: Randall Munroe Fiction 20/08/2019 4
    C123 The Way of the Peaceful Warrior Dan Millman Spiritual 01/09/2000 5
    SELECT BookID, Title, Genre, Rating, PublishDate
           FROM Books
           WHERE Rating > 5 OR PublishDate >= #01/01/1970#
           ORDER BY Title ASC;
    
    • This SQL query will return the following records:

    3.7.2_DB_SQL2.png>

    Standard SQL Operators

    • The standard operators used in SQL are shown below.
    Operator Function Example
    = Equal to Title = "Mr"
    <> Not equal to Title <> "Ms"
    < Less than Rating < 5
    > Greater than Rating > 2
    <= Less than or equal to DoB <= #01/01/1970#
    >= Greater than or equal to DoB >= #01/01/2000#
    IN Equal to a value within a set of values Genre IN ("Fiction", "Non-Fiction" , "Spiritual")
    BETWEEN …AND Within a range, including the two values which define the limits DoB BETWEEN #01/01/2018# AND #31/12/2019#
    IS NULL Field does not contain a value DoB IS NULL
    NOT Inverts truth Genre NOT IN ("Fiction", "Spiritual")
    AND Both expressions must be true for the expression to be true Title = "Mr" AND DoB > #01/01/2000#
    OR If either or both expressions ate true, then the expression is true Title = "Mrs" OR City = "Bourne"

    SQL- SELECT FROM WHERE

    • Lets do some more SQL by working through the following SQL SELECT FROM WHERE Tutorial using theW3 Schools SQL link below then complete the Exercises:

    When you have completed the Exercises go on to the SQL AND, OR, NOT and ORDER BY tutorials and complete the exercises. Then attempt to complete the questions below.

    Questions: SQL- SELECT

    • Try practising your SQL skills by working through the following SQL Select Questions using theW3 Schools SQL link below :
    1. Write a Query that selects all the columns(fields) from the Employees table.
    2. Write a statement that will select the City column from the Customers table.
    3. Select all records from the Customers table where the City column has the value "Berlin".
    4. Select all records from the Customers table where the CustomerID column has the value 32
    5. Select all records from the Customers table where the City column has the value 'Berlin' and the PostalCode column has the value 12209.
    6. Select all records from the Customers table where the City column has the value 'Berlin' or 'London'.
    7. Select the city column from the Customers table, sort the result alphabetically by the column City.
    8. Select the city column from the Customers table, sort the result reversed alphabetically by the column City.
    9. Select the name,address,country and city fileds from the Customers table, sort the result alphabetically, first by the column Country, then, by the column City.
    10. Create a Query that only selects the ProductID 11 from the OrderDetails database. How many orders are there?
    11. Show all of the suppliers that are based in the UK. How many records are there?

    Task 3 - Book Task Click to see more

    Task:

    Open your student workbook at page 11 Read through the notes then complete the following tasks in your book.
    1. Tasks 4

  • When you are finished screenshot your answer into your Learning Journal

  • Task 4 - End of Task Assessment Click to see more