Database All 2
Database All 2
in Business
Department of Information Technology
Fundamental Concepts
Data, Information, Knowledge
• Data
– facts that have implicit meaning
• Information
– Data accompanied with semantics (i.e. the meaning
of data or the context)
– Outcome of a data processing
• Knowledge
– An outcome of reasoning based on credible
information
Database
A database is a collection of related data that has
the following properties:
1. It represents a selected part of the real world,
called the mini-world;
2. It is a logically coherent collection of data with
some inherent meaning;
3. It is designed and used for a specific purpose;
4. It has an intended group of users.
Examples of a database
• Banking database v e.g. clients, accounts,
money transfers;
• Company database v e.g. employees,
departments, payroll;
• Public administration database v e.g. names,
addresses, real estates, taxes;
• Healthcare database v e.g. patients, doctors,
treatments, prescriptions.
Database Management System (1)
A Database Management System (DBMS) is a
collection of software that facilitates the following
operations on the database:
1. defining a database t specifying the datatypes
and structures of data;
2. constructing a database t storing the data;
3. manipulating a database t querying and updating
the database;
4. sharing a database t controlling the simultaneous
user access to the database.
Database Management System (2)
SELECT
1101000011
names FROM
students;
Data Daniels
Jackson
Johnson
Database Walker
Database User
Management
System
Database System
• Database System
Database Management System
+
Database
• IT System
Database System
+
U [~o]]}v
Database Access Language
• DBMS provides data access through a query
language
• Query language allows to declare data
properties (finding and processing data is the
task of DBMS)
• SQL (pronounced: 1li d]1o}] ]dl`/o )
– Structured Query Language (declarative language)
Relational Data Model
Relational Data Model
• Provides a database as a collection of tables (relations)
• Tables t logical structures in which data is stored
• Popular u}o(}}˙ [}]}v
• Implemented through a Relational Database Management
System (RDBMS)
Database
Table: EMPLOYEES
EMP_ID EMP_FIRST_NAME EMP_LAST_NAME EMP_SALARY EMP_DEP_ID
100 John Smith 12000 10
101 Jessica Torn 9000 10
102 John Ford 9000 10
103 Diana Smith 8000
Superkey
• An attribute (or combination of attributes) that uniquely identifies
each row in a table
• Examples
– Emp_Id
– Emp_Id, Emp_Last_Name
– Emp_Id, Emp_Last_Name, Emp_Salary
– Emp_Id t with or without additional attributes can be a
superkey
Table: EMPLOYEES
EMP_ID EMP_FIRST_NAME EMP_LAST_NAME EMP_SALARY EMP_DEP_ID
100 John Smith 12000 10
101 Jessica Torn 9000 10
102 John Ford 9000 10
103 Diana Smith 8000
Primary Key
• Primary key
– A minimal superkey selected to uniquely identify any given
row in a particular table
– May not contain null values
Table: EMPLOYEES
EMP_ID EMP_FIRST_NAME EMP_LAST_NAME EMP_SSN EMP_SALARY EMP_DEP_ID
100 John Smith 481-20-4585 12 000 10
101 Jessica Torn 772-18-2289 9 000 10
102 John Ford 573-75-0204 9 000 10
Null value 103 Diana Smith 441-80-2888 8 000
• Null = Void
• Not a zero
• Not a space
• Even not a zero-length string
Foreign Key
• Attribute whose values match the primary key values in the related
table
• In some cases foreign key may be null
Table: EMPLOYEES
EMP_ID EMP_FIRST_NAME EMP_LAST_NAME EMP_SALARY EMP_DEP_ID
100 John Smith 12 000 10
101 Jessica Torn 9 000 20
EMP_DEP_ID – foreign key 102 John Ford 9 000 10
103 Diana Smith 8 000 30
in the EMPOYEES table
DEP_ID – primary key
in the DEPARTMENTS Table: DEPARTMENTS
table DEP_ID DEP_NAME DEP_LOCATION
10 Marketing 4th Floor
20 Sales 1st Floor
30 Human Resources 5th Floor
Unique Key
• Unique key
– An attribute (or a combination of attributes) whose
all values in a given table must be unique
– May contain null values
• Primary and unique key
– Both keys have to be unique
– Primary key may not contain null values
Unique key may contain null values
Relational Keys t Summary
Key Type Definition
Superkey An attribute (or combination of attributes) that uniquely
identifies each row in a table
Treatment
Prescription
Patient Doctor
Definig the database
t the relationships
Treatment
can result in many
Patient
must realate to one Doctor
Prescription
Definig the database
t the tables
Treatment
Prescription
Patient Doctor
Database
DBMS
3. Resulting data is displayed
LAST_NAME
Smith
Torn
Ford Database
Wilson
Kent
Client Server
side side
SELECT Statement (1/2)
SELECT [DISTINCT]
{*|column|expression
[alias],...}
FROM table
[WHERE condition(s)]
[ORDER BY ...];
• SELECT t specifies columns to be displayed
• FROM table t specifies the table containing the
columns specified in the SELECT clause
• WHERE t limits selection results to rows that meet
a specified condition
SELECT Statement (2/2)
• DISTINCT t remove duplicates from the result
and sorts rows ascending
• * t selects all columns
• column t select a named column
• alias t gives an alternative name for a column
• condition(s) t conditional expression consisting
of column names, constants, and other
expressions
• ORDER BYt sorts rows by the specified values
Guidelines on SQL Statements
• SQL statements are not case sensitive
• You can enter SQL statements on one or more
lines
• Place clauses on separate lines for readability
and ease of editing
• Use tabs and indents to make code more
readable
• To enhance readability enter keywords in
uppercase and all other words in lowercase
Sample database - tables
Tables:
• EMPLOYEES
• DEPARTMENTS
• POSITIONS
• JOB_GRADES
Sample database - relationships
EMPLOYEE
Sample database - content
• Display description of a sample table.
Use DESCRIBE keyword.
• Display content of all sample tables.
Use SELECT * clause and FROM keyword.
Selection
• To retrieve selected rows
SELECT *
FROM employees
WHERE emp_id=101 OR emp_id=103;
Table: EMPLOYEES
EMP_ID FIRST_NAME LAST_NAME SALARY DEP_ID
100 John Smith 12000 10
101 Jessica Torn 9000 10
102 Joe Ford 9000 10
selected
103 Diana Wilson 8000 10 rows
Selection - exercises
• Display all information about employee with id
equal to 104.
Table: EMPLOYEES
EMP_ID FIRST_NAME LAST_NAME SALARY DEP_ID
100 John Smith 12000 10
101 Jessica Torn 9000 10
102 Joe Ford 9000 10
103 Diana Wilson 8000 10
selected
columns
Projection - exercises
• Display the last name and hire date for each
employee.
selected
columns
Selection & Projection exercises
• Display the last name and allowance for each
employee working in department with dep_id
equal to 30.
• Concatenation operator: ||
SELECT 'First name: ' || first_name,
'Last name: ' || last_name
FROM employees;
Basic Operators - exercises
• Display the first name, last name, and annual
salary for all employees.
SELECT SUBSTR(name,1,4)
FROM departments;
SELECT MONTHS_BETWEEN(SYSDATE,
TO_DATE('2020-01-01','YYYY-MM-DD')) "months from the
beginning of 2020"
FROM dual;
Result:
SELECT AVG(salary) AVG(salary)
FROM employees; 6590,00
One value for the whole table
Group Functions with
GROUP BY clause
EMP_ID FIRST_NAME LAST_NAME SALARY DEP_ID
100 John Smith 15500 10
101 Jessica Torn 8800 10
102 Nick Ford 9800 10
emp_dep_id=10
103 Diana Wilson 7900 10
104 Jennifer Kent 5800 20
105 David Brown 3100 20 emp_dep_id=20
106 Nicole Foster 1800 20
107 Steven Anderson 5900 30
108 Lisa Davis 4200 30 emp_dep_id=30
109 Anne Taylor 3100 30
Result:
DEP_ID AVG(salary)
SELECT dep_id, AVG(salary)
10 10500,00
FROM employees
GROUP BY dep_id; 20 3566,67
30 4400,00
• Example:
SELECT dep_id, AVG(salary)
FROM employees
GROUP BY dep_id
HAVING MAX(salary) > 6000;
Group Functions in Action
• No grouping column in a SELECT clause
SELECT AVG(salary), MIN(salary), MAX(salary)
FROM employees
GROUP BY dep_id;
• Grouping by more than one column
SELECT dep_id, jg_id, AVG(salary)
FROM employees
GROUP BY dep_id, jg_id;
• Group functions in ORDER BY clause
SELECT dep_id, AVG(salary)
FROM employees
GROUP BY dep_id
ORDER BY AVG(salary);
Exercises
• Display the manager_id and the salary of the
lowest paid employee for that manager. Exclude
anyone whose manager is not known. Exclude any
groups where the minimum salary is less than
3000. Sort the output in descending order of salary.
• Write a query that displays the total number of
employees hired each year. Label the first column
as "Employees hired" and the other one as "Year".
• Create a query to display the jg_id, the average
salary for that grade based on department id for
departments 20, 30, and 50, giving each column an
appropriate heading
References
• J. Price, Oracle Database 12c SQL, McGraw-Hill
Education, 2013
• Oracle 12c Documentation
http://www.oracle.com/pls/db121/homepage
Thank you!
Database Technology
in Business
Department of Information Technology
Summary of previous topics
Tables
• Data in the relational databases are stored in
tables.
• A table is a data structure that consists of
columns and rows.
• Each column has a name and a data type
assigned.
Data types
• Examples of data types:
– NUMBER
– VARCHAR2
– DATE
• The data stored in the column must match the data
type specified for this column e.g.
– ZvucJ}Zv_vv}]v}co˙ _}ouv`]Z
NUMBER(7,2) data type
– the number vv}]v}co˙ _}ouv`]Z
NUMBER(7,2) data type. This data type accepts a total
number of seven digits with two digits reserved for decimal
part and five digits for the integer part.
Out of the scope of the
NUMBER(7,2) datatype 120 000, 00 This number requires at least
NUMBER(8,2) datatype
Resulting view
FIRST_NAME LAST_NAME NAME LOCATION
John Smith Executive Room 306
Jessica Torn Executive Room 306
Joe Ford Marketing Room 201
Equijoin
• Values in one column of the first table must be
equal to the values in another column in the
second table
• Syntax
SELECT first_name, last_name, name, location
FROM employees e JOIN departments d
ON (e.dep_id = d.dep_id);
Inner & Outer Join
• Inner join t the join of two tables returning
only matched rows
• Left (right) outer join t the join of two tables
returning both matched and rows unable to be
matched from the left (right) table
• Full outer join t the join of two tables
returning the result of an inner join and the
rows unable to be matched from the left and
right tables
Outer Join Syntax
• Left outer join
SELECT e.last_name, d.name, d.location
FROM employees e LEFT OUTER JOIN departments d
ON (e.dep_id = d.dep_id);
FROM employees e
JOIN departments d ON (e.dep_id = d.dep_id)
JOIN job_grades jg ON (e.jg_id = jg.jg_id);
Exercises
1. Write a query to display the last name,
department id, and department name for all
employees who work in any department.
Order the results by the department name.
2. Display the last name, salary, and department
name of all employees who earn salary
greater than 4000 and earn an allowance.
3. Display the last name, salary for all employees
working in the department 'Operations'.
Exercises
4. Write a query to display the last name, job grade
name, and department name for all employees
(including those with no department) with the job
grade whose a minimum salary boundary is lower
than 4000.
5. Write a query to display the last name, department
name, and department location for all employees
who work in a room (office) which number is greater
than 200.
6. For employees in departments 20 or 30, display
the last name, department name, the name of
the employee's manager and department name of
their managers.
Exercises
7. Find all employees who joined the company
before their manager and display their last
name, hire date ~ooc emp Z]_
and hire date of their manager (labeled as
cuvPZ]_X
8. Display the last name and department name
for all employees including those who are not
assigned to any department. Replace the null
values in the department name column with
the string 'No department'.
Exercises
9. Display the last name and the last name of the
manager ~oocuvP _UvZ
department name for all employees who work
in the same department as their manager.
10.Create a query that for each employee displays
the last name, department id, and all the last
names of the employees who work in the
same department as a given worker. Give each
column an appropriate label.
References
• J. Price, Oracle Database 12c SQL, McGraw-Hill
Education, 2013
• Oracle 12c Documentation
http://www.oracle.com/pls/db121/homepage
Thank you!
Database Technology
in Business
Department of Information Technology
Subqueries I
Subquery
• SELECT statement embedded in another SELECT
statement (main query)
• Subquery executes and its output is used to
complete the condition in the main query
• Subquery synonyms: embedded query, inner query,
sub-SELECT
• Example
SELECT last_name
FROM employees
WHERE salary > (SELECT salary
FROM employees
WHERE last_name = 'Parker');
Types of Subqueries
• Single-row / Multiple-row
– Return single / multiple row(s)
• Single-column / Multiple-column
– Return single / multiple column value(s)
• Scalar subqueries
– Return exactly one value at one row
Operators for Subquery Conditions
• Single-row operators
– >, =, >=, <, <>, <=
• Multiple-row operators
– >, =, >=, <, <>, <= ANY, ALL
– [NOT] IN
– [NOT] EXISTS
• Guidelines for specifying subqueries:
– Subquery is enclosed in parentheses
– Subquery appears on the right side of the
comparison operator
– Single-row operator can be used with a single-row
subquery only
Subquery Categories
• Non-correlated
– Executed once before the main query
– S˙ [}]} complete the
condition for the main query
• Correlated
– Executed once for each candidate row at the main
query
– S˙ [}]} complete the
condition for the main query and filter its output
Non-correlated Subquery
• S˙ [}] independent from the main query
• Subquery is executed and returns the output
• Main query is executed once using the output returned by
the subquery
Main query
Which employees have a salary Subquery output
greater than Parker does?
Subquery
What’
s Parker’ssalary
?
Non-correlated Subquery Syntax
• Can be used in WHERE, HAVING, FROM
• General syntax in WHERE clause
SELECT select_list
FROM table1 Main query
WHERE expr operator
(SELECT select_list
Subquery
FROM table2);
Examples of Single-row
Non-correlated Subqueries (1/2)
• WZ]Zuo}˙Zo˙PZvPl[o˙ ?
SELECT last_name
FROM employees
WHERE salary > (SELECT salary
FROM employees
WHERE last_name = 'Parker');
• Which employees have the same manager as Torn does?
SELECT last_name, manager_id
FROM employees
WHERE manager_id = (SELECT manager_id
FROM employees
WHERE last_name = 'Torn');
Examples of Single-row
Non-correlated Subqueries (2/2)
• Which employees hired in the same department as the employee
of id=110 earn more than the employee of id=112?