Selecting Data (Single Table)

What is SQL?

  • SQL (Structured Query Language) is a programming language used to interact with a DBMS.
  • The use of SQL allows a user to:
    • Select data (single table)
    • Select data (multiple tables)
    • Insert data
    • Delete records
    • Delete tables

Selecting data (single table) commands

CommandDescriptionExample
SELECTRetrieves data from a database tableSELECT * FROM users;
(retrieves all data from the ‘users’ table)

SELECT name, age
FROM users
(retrieves names and ages from the ‘users’ table)
FROMSpecifies the tables to retrieve data fromSELECT name, age FROM users;
(retrieves names and ages from the ‘users’ table)
WHEREFilters the data based on a specified conditionSELECT * FROM users
WHERE age > 30;
(Retrieves users older than 30)
LIKEFilters the data based on a specific patternSELECT * FROM users
WHERE name LIKE ‘J%’;
(retrieves users whose names start with ‘J’)
ANDCombines multiple conditions in a WHERE clauseSELECT * FROM users
WHERE age > 18 AND city = ‘New York’;
(retrieves users older than 18 and from New York)
ORRetrieves data when at least one of the conditions is trueSELECT * FROM users
WHERE age < 18 OR city = ‘New York’;
(retrieves users younger than 18 or from New York)
WILDCARDS'' and ’%’ symbols are used for searching and matching data
'
' used to select all columns in a table
’%’ used as a wildcard character in the LIKE operator
SELECT * FROM users;
(retrieves all columns for the ‘users’ table)

SELECT * FROM users WHERE name LIKE ‘J%’;
(retrieves users whose names start with ‘J’)

Examples

  • Select all the fields from the Customers table

Command:

SELECT * FROM Customers;

Output:

IDNameAgeCityCountry
1John Doe30New YorkUSA
2Jane Doe25LondonUK
3Peter Lee40ParisFrance
  • Select the ID, name & age of customers who are older than 25

Command:

SELECT ID, name, age
FROM Customers
WHERE Age > 25;

Output:

IDNameAge
1John Doe30
3Peter Lee40
  • Select the name and country of customers who are from a country that begins with ‘U’

Command:

SELECT Name, Country
FROM Customers
WHERE COuntry LIKE 'U%';

Output:

NameCountry
John DoeUSA
Jane DoeUK
  • Select all fields of customers who are from ‘London’ or ‘Paris’

Command:

SELECT *
FROM Customers
WHERE City = 'London' OR City = 'Paris';

Output:

IDNameAgeCityCountry
2Jane Doe25LondonUK
3Peter Lee40ParisFrance

Worked Example

Customers’ details are stored in the flat file database table Customer. An extract of the table is shown below

relational-database Write the SQL statement that would show only the CustomerID and Surname fields for customers with the Title “Miss” or “Mrs”

[4]

Answer: SELECT CustomerID, Surname [1] FROM Customer [1] WHERE Title=“Miss” [1] OR Title = “Mrs” [1]

Selecting Data (Multiple Tables)

CommandDescriptionExample
Nested SELECTA select within another select statement (nested). A mini select within the main oneSELECT * FROM users WHERE age > (SELECT AVG(age) FROM users);
(retrieves users with an age greater than the average age)
JOIN
** (INNER JOIN)**
Combines data from two or more tables based on a related columnSELECT users.name, orders.order_id FROM users
JOIN orders
ON users.user_id = orders.user_id;
(retrieves user names and their corresponding order IDs)

Examples

Table: Employees

IDNameSalaryDepartmentCity
1Fynn Roberts45000HRLondon
2Zarmeen Azra52000SalesManchester
3Ella Stanley39500MarketingBirmingham
  • Select all fields for employees whose salary is bigger than the average salary of all employees

Command: code Output:

IDNameSalaryDepartmentCity
2Zarmeen Azra52000SalesManchester

Inserting Data

CommandDescriptionExample
INSERTAdds new data to a database tableINSERT INTO users (name, age)
VALUES (‘John Doe’,25);
(inserts a new user with the name ‘John Doe’ and age 25)

Example

Table: Employees

IDNameSalaryDepartmentCity
1Fynn Roberts45000HRLondon
2Zarmeen Azra52000SalesManchester
3Ella Stanley39500MarketingBirmingham
  • Insert a new employee into the Employees table with the ‘Name’, ‘Salary’, ‘Department’ and ‘City’ fields

Command: code Output:

IDNameSalaryDepartmentCity
1Fynn Roberts45000HRLondon
2Zarmeen Azra52000SalesManchester
3Ella Stanley39500MarketingBirmingham
4George Rope47250SalesLeeds

Deleting Records

CommandDescriptionExample
DELETERemoves data from a database tableDELETE FROM users
WHERE age < 18;
(deletes all users younger than 18 from the ‘users’ table)

DELETE FROM users
WHERE name=“John”;
(deletes a record where the name is John)

Example

Table: Employees

IDNameSalaryDepartmentCity
1Fynn Roberts45000HRLondon
2Zarmeen Azra52000SalesManchester
3Ella Stanley39500MarketingBirmingham
4George Rope47250SalesLeeds
  • Delete all records from the Employees table whose department is ‘Marketing’

Command: code Output:

IDNameSalaryDepartmentCity
1Fynn Roberts45000HRLondon
2Zarmeen Azra52000SalesManchester
3George Rope47250SalesLeeds

Worked Example

A database stores information about songs on a music streaming service. One of the tables called Song has the fields Title, Artist, Genre and Length

A band called RandomBits removes their permission for their songs to be streamed. The company removes all the songs belonging to RandomBits from their service.

Write an SQL statement that will remove all songs by RandomBits from the table Song

[2]

Answer: DELETE FROM Song [1] WHERE Artist = “RandomBits” [1]

Deleting Tables

CommandDescriptionExample
DROPDeletes a table in a databaseDROP TABLE users;
(deletes the ‘users’ table)

Example

Table: Employees

IDNameSalaryDepartmentCity
1Fynn Roberts45000HRLondon
2Zarmeen Azra52000SalesManchester
3Ella Stanley39500MarketingBirmingham
4George Rope47250SalesLeeds
  • Delete the Employees table

Command: code Output code