0% found this document useful (0 votes)
40 views

Big Data Analytics: Database - SQL

This document discusses SQL commands for querying and modifying data in a database. It covers the SELECT, INSERT, UPDATE, DELETE statements. It provides examples of using SELECT to retrieve specific columns and rows from tables, including using the WHERE clause to filter rows based on conditions. It also discusses operators like comparison, IN and BETWEEN that can be used in the WHERE clause to select rows.

Uploaded by

Ali Nasir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Big Data Analytics: Database - SQL

This document discusses SQL commands for querying and modifying data in a database. It covers the SELECT, INSERT, UPDATE, DELETE statements. It provides examples of using SELECT to retrieve specific columns and rows from tables, including using the WHERE clause to filter rows based on conditions. It also discusses operators like comparison, IN and BETWEEN that can be used in the WHERE clause to select rows.

Uploaded by

Ali Nasir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

9/14/2021

Big Data Analytics


Database - SQL
Muhammad Affan Alim

Writing SQL Commands


• In practice, the DDL statements are used to create the
database structure (that is, the tables) and the access
mechanisms (that is, what each user can legally access),
and then the DML statements are used to populate and
query the tables.

1
9/14/2021

Writing SQL Commands


This section looks at the following SQL DML statements:

• SELECT – to query data in the database


• INSERT – to insert data into a table
• UPDATE – to update data in a table
• DELETE – to delete data from a table

Writing SQL Commands


• We illustrate the SQL statements using the instance of the DreamHome case study
shown in Figure, which consists of the following tables:
- Branch (branchNo, street, city, postcode)
- Staff (staffNo, fName, IName, position, sex, DOB, salary, branchNo)
- PropertyForRent (propertyNo, street, city, postcode, type, rooms, rent, ownerNo, staffNo,
branchNo)
- Client (clientNo, fName, IName, telNo, prefType, maxRent, eMail)
- PrivateOwner (ownerNo, fName, IName, address, telNo, eMail, password)
- Viewing (clientNo, propertyNo, viewDate, comment)
- Registration (clientNo, branchNo, staffNo, dateJoined)

2
9/14/2021

Simple Queries
• The purpose of the SELECT statement is to retrieve and display
data from one or more database tables

• It is an extremely powerful command, capable of performing the


equivalent of the relational algebra’s Selection, Projection, and
Join operations in a single statement

Simple Queries
• SELECT is the most frequently used SQL command and has the
following general form:

3
9/14/2021

Simple Queries
• The sequence of processing in a SELECT statement is:
• FROM: specifies the table or tables to be used
• WHERE: filters the rows subject to some condition
• GROUP: BY forms groups of rows with the same column value
• HAVING: filters the groups subject to some condition
• SELECT: specifies which columns are to appear in the output
• ORDER: BY specifies the order of the output

Simple Queries
• The order of the clauses in the SELECT statement cannot be
changed. The only two mandatory clauses are the first two: SELECT
and FROM; the remainder are optional

4
9/14/2021

Simple Queries
• Example Retrieve all columns, all rows
• Because there are no restrictions specified in this query, the
WHERE clause is unnecessary and all columns are required. We
write this query as:

• SELECT staffNo, fName, IName, position, sex, DOB, salary,


branchNo FROM Staff;

Simple Queries
• The following statement is an equivalent and shorter way of
expressing this query: SELECT * FROM Staff;

10

5
9/14/2021

Simple Queries
Example: Retrieve specific columns, all rows
• Produce a list of salaries for all staff, showing only the staff number, the first
and last names, and the salary details.
• SELECT staffNo, fName, IName, salary FROM Staff;

11

Simple Queries

12

6
9/14/2021

Simple Queries

13

Row selection (WHERE clause)


• The previous examples show the use of the SELECT statement to
retrieve all rows from a table

• which consists of the keyword WHERE followed by a search


condition that specifies the rows to be retrieved

• The five basic search conditions are as follows:

14

7
9/14/2021

Row selection (WHERE clause)


• Comparison Compare the value of one expression to the value of
another expression
• Range Test whether the value of an expression falls within a
specified range of values
• Set membership Test whether the value of an expression equals
one of a set of values
• Pattern match Test whether a string matches a specified pattern
• Null Test whether a column has a null (unknown) value.
15

Row selection (WHERE clause)


Example Comparison search condition
List all staff with a salary greater than £10,000

SELECT staffNo, fName, IName, position, salary


FROM Staff
WHERE salary > 10000;

16

8
9/14/2021

Row selection (WHERE clause)


In SQL, the following simple comparison operators are available:
= equals
<> is not equal to ! = is not equal to (allowed in some dialects)
< is less than < = is less than or equal to
> is greater than > = is greater than or equal to
More complex predicates can be generated using the logical
operators AND, OR, and NOT, with parentheses (if needed or
desired) to show the order of evaluation.

17

Row selection (WHERE clause)


The rules for evaluating a conditional expression are:
• an expression is evaluated left to right;
• subexpressions in brackets are evaluated first;
• NOTs are evaluated before ANDs and ORs;
• ANDs are evaluated before Ors
The use of parentheses is always recommended, in order to remove
any possible ambiguities.

18

9
9/14/2021

Row selection (WHERE clause)

19

Row selection (WHERE clause)

20

10
9/14/2021

Row selection (WHERE clause)


s

21

Row selection (WHERE clause)


There is a negated version (NOT IN) that can be used to check for
data values that do not lie in a specific list of values. Like BETWEEN,
the IN test does not add much to the expressive power of SQL.
We could have expressed the previous query as:
SELECT staffNo, fName, IName, position
FROM Staff
WHERE position = ‘Manager’ OR position = ‘Supervisor’;
However, the IN test provides a more efficient way of expressing the
search condition, particularly if the set contains many values.

22

11
9/14/2021

Row selection (WHERE clause)


d

23

Row selection (WHERE clause)


d

24

12
9/14/2021

Row selection (WHERE clause)


d

25

Row selection (WHERE clause)


d

26

13
9/14/2021

Row selection (WHERE clause)


you may think that the latter row could be accessed by using one of
the search conditions:
(propertyNo = ‘PG4’ AND comment = ‘ ’)
or
(propertyNo = ‘PG4’ AND comment < > ‘too remote’)
However, neither of these conditions would work. A null comment is
considered to have an unknown value, so we cannot test whether it
is equal or not equal to another string.
If we tried to execute the SELECT statement using either of these
compound conditions, we would get an empty result table
27

Row selection (WHERE clause)


• If we tried to execute the SELECT statement using either of these
compound conditions, we would get an empty result table
• Instead, we have to test for null explicitly using the special keyword IS
NULL:
SELECT clientNo, viewDate FROM Viewing
WHERE propertyNo = ‘PG4’ AND comment IS NULL;
The result table is shown in Table 6.10. The negated version (IS NOT
NULL) can be used to test for values that are not null.

28

14
9/14/2021

Sorting Results (ORDER BY Clause)


• we can ensure the results of a query are sorted using the
ORDER BY clause in the SELECT statement
• The ORDER BY clause consists of a list of column identifiers that
the result is to be sorted on, separated by commas
• However, some dialects insist that the ORDER BY elements appear
in the SELECT list. In either case, the ORDER BY clause must always
be the last clause of the SELECT statement.

29

Sorting Results (ORDER BY Clause)


30

15
9/14/2021

Sorting Results (ORDER BY Clause)


31

Sorting Results (ORDER BY Clause)


32

16
9/14/2021

Using the SQL Aggregate Functions


• As well as retrieving rows and columns from the database, we
often want to perform some form of summation or aggregation
of data, similar to the totals at the bottom of a report. The ISO
standard defines five aggregate functions:

33

Example of ER diagram construction


d

34

17

You might also like