SQL

  • Records in a database can be searched and data can be manipulated using Structured Query Language (SQL)
  • SQL statements can be written to query the data in the database and extract useful information
  • SQL statements follow this structure:
    • SELECT the fields you want to display 
    • FROM the table/tables containing the data you wish to search
    • WHERE the search criteria 

A Database Table Containing Movie Details


MovieID

Name

Genre

Certificate

Rating
M23MoanaFamilyU8.1
M8Shaun of the DeadComedy188.7
M56Die HardAction188.4
M34BigFamilyPG8.5

Example

SELECT Name, Rating

FROM Movie

WHERE Rating>8.4;

The results of this query would be:


Name

Rating
Shaun of the Dead8.7
Big8.5
  • The two fields –  Name and Rating have been extracted from the Movie table and then the records have been filtered by Rating
  • This example uses the > operator to search for records where the rating is greater than 8.4
  • There are several other comparison operators which can be used to create the filter criteria in the WHERE line of a SQL query

SQL Comparison Operators


Operator

Description
>Greater than
>=Greater than or equal to
<Less than
<=Less than or equal to
=Equal to
<>Not equal to

Example

SELECT Name,Rating

FROM Movie

WHERE Genre=”Family” AND Certificate=”U”;

The results of this query would be:


Name

Rating
Moana8.1
  • The two fields Name and Rating have been extracted from the Movie table and the records have been filtered by both Genre and Certificate
  • This query uses the AND logical operator to include multiple criteria in the WHERE line of the SQL query
  • Another logical operator which can be used in the WHERE statement is OR
    • For example, WHERE Genre=”Comedy” OR Genre=”Family”

ORDER BY

  • You can enter a fourth line to the statement using the ORDER BY command, followed by ASC or DESC
    • If you enter ASC the results of the query will be sorted in ascending order
    • If you enter DESC the results of the query are sorted in descending order

Example

SELECT Name,Genre, Certificate, Rating

FROM Movie

ORDER BY Name ASC

  • The results of this query would be:

Name

Genre

Certificate

Rating
BigFamilyPG8.5
Die HardAction188.4
MoanaFamilyU8.1
Shaun of the DeadComedy188.7
  • The query has returned four fields and all records because there were no WHERE criteria. The records are sorted by Name alphabetically
    • If numbers are sorted in ascending order they go from the lowest number at the top of the table to the highest number at the bottom
    • Descending order is the highest number to the lowest

SUM and COUNT commands

  • The SUM command can be used to add numerical data
  • The COUNT command can be used to count items of data

ProductID

ProductName

Price

QuantityInStock
1Sausages1.993
2Chips2.992
3Beans2.505
4Bananas2.1012
5Avocado1.003

Example

SELECT SUM(QuantityInStock)

FROM ProductTable;

  • This query will add up all of the numbers in the QuantityInStock field
    • The result of this query would be 25 

Example

SELECT COUNT(*)

FROM ProductTable

WHERE Price>2;

  • This query will count all the records with a price greater than 2
    • The result of this query would be 3
    • This is because there are three products with a price greater than £2 (Chips, Beans, Bananas)

Loading

error: Content is protected !!