CS计算机代考程序代写 SQL database finance AI Before we Begin…

Before we Begin…

2

● Most SQL examples in this slide deck refer to the university
database (uni_small or uni_large) which have been pre-loaded
in your Codio Box.

● It is recommended that you try all examples on your own.
● Remember that you can’t “break” the Codio Box and at any point

you can reset it to its original state.
○ Especially when working with the INSERT and UPDATE

statements, you will alter the default database. Make sure you
reset the Codio Box before answering the homework
assignments.

SQL Components (review)

3

● Data-definition language (DDL): allows the specification of
information about relations, including:
○ The schema for each relation.
○ The type of values associated with each attribute.
○ The integrityconstraints
○ The set of indices to be maintained for each relation.
○ Security and authorization information for each relation.
○ The physical storage structure of each relation on disk.

● Data-manipulation language (DML): provides the ability to query
information from the database and to insert tuples into, delete
tuples from, and modify tuples in the database.

SQL Components (review)

4

● Integrity: the DDL includes commands for specifying integrity
constraints (i.e. primary & foreign keys).

● View definition: The DDL includes commands for defining views.
● Transaction control: includes commands (like, COMMIT, ROLLBACK,

SETPOINT, SAVE TRANACTIONS) for specifying the beginning and
ending of transactions.

● Embedded SQL and dynamic SQL: define how SQL statements can
be embedded within general-purpose programming languages.

● Authorization: includes commands for specifying access rights to
relations and views.

Data and Domain Types: Why?

Consistency

Validation

Compactness

Performance

Basic Domain Types (Data Types)

6

● char(n) –Fixed length character string, with user-specified length
n. (also character(n))

● varchar(n) –Variable length character strings, with user-specified
maximum length n. (also character varying(n))

● int –Integer (a finite subset of the integers that is machine
dependent).

● smallint –Small integer (a machine-dependent subset of the
integer domain type).

● numeric(p,d) –Fixed point number, with user-specified precision
of p digits, with d digits to the right of decimal point.
i.e.: numeric(3,1), allows 44.5 to be stored exactly, but not 444.5
or 0.32

For more info: https://www.postgresql.org/docs/11/datatype.html

https://www.postgresql.org/docs/11/datatype.html

Basic Domain Types (Data Types) (cont.)

7

● real, double precision –Floating point and double-precision floating
point numbers, with machine-dependent precision.

● Visit PostgreSQL documentation:
https://www.postgresql.org/docs/11/datatype.html

https://www.postgresql.org/docs/current/datatype-
numeric.html

https://www.postgresql.org/docs/11/datatype.html
https://www.postgresql.org/docs/current/datatype-numeric.html

Create Table Construct

8

● A SQL table is defined using the CREATE TABLE command:

CREATE TABLE r
(A1D1, A2D2, …, AnDn,
(integrity-constraint1),

…,
(integrity-constraintk))

○ r is the name of the relation (table)
○ each Ai is an attribute name in the schema of relation r
○ Di is the data type of values in the domain of attribute Ai

Classwork-1: Create Table Construct

9

Open query tool on UNI_SMALL databse. Provide the SQL statement that creates a
table named “instructors” with the following attributes: “id” (fixed length character
string of 5), “name” (variable length character string of 20), “dept_name” (variable
length character string of 20), “salary”(fixed point number with 8 digits, 2 decimals)

Integrity Constraints in CREATE TABLE

Types of integrity constraints:

1 n

m n

● NOT NULL
● PRIMARY KEY (A , …, A )
● FOREIGN KEY (A , …, A ) references r

Example:

9PRIMARY KEY declaration on an attribute automatically ensures NOT NULL

CREATE TABLE instructor (
id char(5),
name varchar(20) NOT NULL,
dept_name varchar(20),
salary numeric(8,2),
PRIMARY KEY (ID),
FOREIGN KEY (dept_name) REFERENCES department);

Classwork: Define Integrity Constraints in
CREATE TABLE

Example:

CREATE TABLE instructors ( id char(5) primary
key,

name varchar(20),

dept_name varchar(20) references
department(dept_name),

Salary numeric(8,2));

In PostgreSQL Syntax

Define the following integrity constrain in the instructors relation we created.

Set ‘id’ as a primary key and ‘dept_name’ as a foreign key

10

Practice: And a Few More Table Definitions

CREATE TABLE honor_students (
ID varchar(5),
name varchar(20) NOT NULL,
dept_name varchar(20),
tot_cred numeric(3,0),
PRIMARY KEY (ID),
FOREIGN KEY (dept_name) REFERENCES department

);

CREATE TABLE course (
course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0),
PRIMARY KEY (course_id),
FOREIGN KEY (dept_name) REFERENCES department

);

Practice: And a Few More Table Definitions (cont.)

13

Note: sec_id can be dropped from PRIMARY KEY above, to ensure a student cannot be
registered for two sections of the same course in the same semester

CREATE TABLE takes (
ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year integer,
grade varchar(2),
PRIMARY KEY (ID, course_id, sec_id, semester, year),
FOREIGN KEY (ID) REFERENCES student,
FOREIGN KEY (course_id, sec_id, semester, year)

REFERENCES section
);

Classwork: Updates to the table instructors

14

● Insert
INSERT INTO instructors VALUES (
‘10211’, ‘Smith’, ‘Biology’, 66000);

● Delete
○ Remove all tuples from the instructors relation:

DELETE FROM
instructors;

● DropTable
DROP TABLE
instructors;

Updates to Tables (cont.)

15

● Alter
○ alter table r add AD

■ where A is the name of the attribute to be added to relation
r and D is the domain of A.

■ all exiting tuples in the relation are assigned null as the
value for the new attribute.

○ alter table r drop A
■ where A is the name of an attribute of relation r
■ dropping of attributes not supported by many databases.

Classwork2: Updates to Tables (cont.)

16

Provide the SQL statement that alters relation “instructors” adding an
attribute called “work_exp” (smallint):

Basic Query Structure

17

● A typical SQL query has the form:

SELECT A1, A2, …, An
FROM r1, r2, …, rm
WHERE P

○ Ai represents an attribute (column)
○ ri represents a relation (table)
○ P is a predicate.

● The result of an SQL query is a relation.

The SELECT Clause

18

● The select clause lists the attributes desired in the result of a query
(it corresponds to the projection operation of the relational algebra)

Example: find the names of all instructors:

● Note: SQL names are case insensitive (i.e., you may use upper- or
lower-case letters.)
○ i.e.: Name ≡ NAME ≡ name
○ Some people use upper case wherever we use bold font.

SELECT name FROM instructor;

The SELECT Clause (cont.)

19

● SQL allows duplicates in relations as well as in query results.
● To force the elimination of duplicates, insert the keyword DISTINCT

after SELECT.
● Find the department names of all instructors, and remove

duplicates:

● The keyword ALL specifies that duplicates should not be removed.

SELECT DISTINCT dept_name
FROM instructor;

SELECT ALL dept_name
FROM instructor;

The SELECT Clause (cont.)

20

● An asterisk in the select clause denotes “all attributes”

● An attribute can be a literal with no FROM clause

○ Results is a table with one column and a single row with value
“437”

○ Can give the column a name using:

SELECT *
FROM instructor;

SELECT ‘437’;

SELECT ‘437’ AS FOO;

The SELECT Clause (cont.)

21

● An attribute can be a literal with a FROM clause

● Result is a table with one column and N rows (number of tuples in
the instructors table), each row with value “A”

SELECT ‘A’
FROM instructor;

The SELECT Clause (cont.)

22

● The SELECT clause can contain arithmetic expressions involving the
operation, +, –, * , and / , and operating on constants or attributes
of tuples.
○ The query:

would return a relation that is the same as the instructor
relation, except that the value of the attribute salary is divided
by 12.

○ Can rename “salary/12” using the AS clause:

SELECT id, name, salary/12
FROM instructor;

SELECT id, name, salary/12 AS monthly_salary
FROM instructor;

The WHERE Clause

23

● The WHERE clause specifies conditions that the result must satisfy
○ Corresponds to the selection predicate of the relational algebra.

● To find all instructors in ‘Comp. Sci.’ department:

● Comparison results can be combined using the logical connectives
AND, OR, and NOT (also <, <=, >, >=, =, and <>)
○ To find all instructors in ‘Comp. Sci.’ dept with salary > 90000

● Comparisons can be applied to results of arithmetic expressions.

SELECT name
FROM instructor
WHERE dept_name = ‘Comp. Sci.’;

SELECT name
FROM instructor
WHERE dept_name = ‘Comp. Sci.’ AND salary > 90000;

Class Work-3: WHERE Clause

Find the titles of courses in Comp.Sci department that have 3
credits:

21

The FROM Clause

● The FROM clause lists the relations involved in the query
○ Corresponds to the Cartesian product operation of the relational

algebra.
● Find the Cartesian product of instructor Xteaches:

○ generates every possible instructor – teaches pair, with all
attributes from both relations.

○ For common attributes (i.e., ID), the attributes in the resulting
table are renamed using the relation name (i.e.,
instructor.ID)

● The cartesian product is not very useful directly, but useful
combined with where-clause condition (selection operation in
relational algebra).

SELECT * FROM instructor, teaches;

Classwork: Examples

26

● Find the names of all instructors who have taught some course and
the course_id:

● Find the names of all instructors in the Physics department who
have taught some course and the course_id:

SELECT name, course_id
FROM instructor, teaches
WHERE instructor.ID = teaches.ID;

SELECT name, course_id
FROM instructor, teaches
WHERE instructor.ID = teaches.ID
AND instructor.dept_name = ‘Physics’;

Challenge Question:

Find id and names of instructors who have never given A grade?

The Rename Operation

28

● The SQL allows renaming relations and attributes using the AS
clause:

● Find the names of all instructors who have a higher salary than
some instructor in ‘Comp. Sci’:

Note: Keyword AS may be omitted in the FROM clause.

old_name AS new_name

SELECT DISTINCT T.name
FROM instructor AS T, instructor AS S
WHERE T.salary > S.salary
AND S.dept_name = ‘Comp. Sci.’;

Class Work-4

29

Assume table emp_super:

1. Write a query to find the supervisor of ‘Bob’
2. Write a query to find the supervisor of the supervisor of ‘Bob’

person supervisor
Bob Alice

Mary Susan

Alice David

David Mary

Classwork Answers

select supervisor from emp_super e
where person = ‘Bob’;

select supervisor from emp_super e
where e. person in (select supervisor from emp_super e
where person = ‘Bob’);

Challenge Question 2?

Advanced: Can you find ALL the supervisors (direct and
indirect) of “Bob”?

String Operations

32

SQL includes a string-matching operator for comparisons on character
strings. The operator LIKE uses patterns that are described using two
special characters:
●percent ( % ). The % character matches anysubstring.
●underscore ( _ ). The _ character matches any character.
Examples:
● Find the names of all instructors whose name includes the substring

‘El’:

● Match the string ‘100%’ (backslash ‘ \ ’ is the escape character)

SELECT name
FROM instructor
WHERE name LIKE ‘%El%’;

LIKE ‘100\%’ ESCAPE ‘\’

String Operations (cont.)

33

● Patterns are case sensitive.
● Pattern matching examples:

○ ‘Intro%’ matches any string beginning with “Intro”.
○ ‘%Comp%’ matches any string containing “Comp” as a

substring.
○ ‘_ _ _’ matches any string of exactly three characters.
○ ‘_ _ _ %’matches any string of at least three characters.

● SQL supports a variety of string operations such as
○ concatenation (using “||”)
○ converting from upper to lower case (and vice versa)
○ finding string length, extracting substrings, etc.

For more info: https://www.postgresql.org/docs/11/functions-string.html

https://www.postgresql.org/docs/11/functions-string.html

Ordering the Display of Tuples

34

● List in alphabetical order the names of all instructors

● We may specify DESC for descending order or ASC forascending
order, for each attribute (default is ascending)

● Can sort on multiple attributes

SELECT DISTINCT name
FROM instructor
ORDER BY name;

ORDER BY name DESC

ORDER BY dept_name, name

Classwork: WHEREClause Predicates

35

● SQL includes a BETWEEN comparison operator
● Example: Find the names of all instructors with salary between

$90,000 and $100,000 (that is, $90,000 and $100,000)

● Tuple comparison

SELECT name
FROM instructor
WHERE salary BETWEEN 9000 AND 100000;

SELECT name, course_id
FROM instructor, teaches
WHERE (instructor.ID, dept_name) = (teaches.ID, ‘Biology’);

Null Values

36

● It is possible for tuples to have a null value, denoted by NULL, for
some of their attributes

● NULL signifies an unknown value or that a value does not exist.
● The result of any arithmetic expression involving NULL is NULL

○ Example: 5 + NULL returns NULL
● The predicate IS NULL can be used to check for NULL values.

○ Example: Find all instructors whose salary IS NULL.

SELECT name
FROM instructor
WHERE salary IS NULL;

Null Values and Three Valued Logic (true, false, unknown)

● Any comparison with NULL returnsunknown
○ Example: 5 < NULL or NULL <> NULL or NULL =NULL

● Three-valued logic using the value unknown:
○ OR: (unknownor true) = true,

(unknown or false) = unknown
(unknown or unknown) = unknown

○ AND: (trueand unknown) = unknown,
(false and unknown) = false, (unknown
and unknown) = unknown

○ NOT: (not unknown) = unknown
○ “P is unknown” evaluates to true if predicate P evaluates to

unknown
● Result of WHERE clause predicate is treated as false if it evaluates

30 to unknown

Aggregate Functions

38

● Aggregate functions operate on the multiset of values of a column
of a table, and return a value

AVG:
MIN:
MAX:
SUM:

average value
minimum value
maximum value
sum of values

COUNT: number of values

● Find the average salary of instructors in the Computer Science
department:

SELECT AVG (salary)
FROM instructor
WHERE dept_name = ‘Comp. Sci.’;

Classwork: Aggregate Functions (cont.)

39

● Find the total number of instructors who teach a course in the
Spring 2010 semester

● Find the number of tuples in the course table

SELECT COUNT (DISTINCT ID)
FROM teaches
WHERE semester = ‘Spring’
AND year = 2010;

SELECT COUNT (*)
FROM course;

GROUP BY

● Find the average salary of instructors in each department:
SELECT dept_name, AVG(salary) AS avg_salary
FROM instructor
GROUP BY dept_name;

Note:
Attributes in
SELECT clause
outside of
aggregate functions
must appear in
GROUP BY list

40

HAVING

41

Find the names and average salaries of all departments whose average
salary is greater than 42000:

Note: predicates in the HAVING clause are applied after the formation of
groups whereas predicates in the WHERE clause are applied before
forming groups

SELECT dept_name, AVG(salary)
FROM instructor
GROUP BY dept_name
HAVING AVG(salary) > 42000;

Null Values and Aggregates

42

● Total all salaries

○ Above statement ignores NULL amounts
○ Result is NULL if there is no non-nullamount

● All aggregate operations except COUNT(*) ignore tuples with NULL
values on the aggregated attributes

● What if collection has only NULL values?
○ COUNT returns 0
○ All other aggregates return NULL

SELECT SUM(salary)
FROM instructor;

Nested Subqueries

43

SQL provides a mechanism for the nesting of subqueries. A subquery is
a SELECT-FROM-WHERE expression that is nested within another query.
The nesting can be done in the following SQL query,

SELECT A1, A2, …,An
FROM r1, r2, …, rm
WHERE P

as follows:
i● A can be replaced be a subquery that generates a single value.

i● r can be replaced by any valid subquery
● P can be replaced with an expression of the form:

B (subquery)
Where B is an attribute and to be defined later.

Classwork: Set Membership

44

Find courses offered in Fall 2009 and in Spring 2010:

SELECT DISTINCT course_id
FROM section
WHERE semester = ‘Fall’ AND year = 2009 AND

course_id IN (SELECT course_id
FROM section
WHERE semester = ‘Spring’
AND year = 2010);

Classwork: Set Membership (cont.)

45

Find courses offered in Fall 2009 but not in Spring 2010:

SELECT DISTINCT course_id
FROM section
WHERE semester = ‘Fall’ AND year = 2009 AND

course_id NOT IN (SELECT course_id
FROM section
WHERE semester = ‘Spring’
AND year = 2010);

Classwork: Set Membership (cont.)

46

Find the total number of (distinct) students who have taken course
sections taught by the instructor with ID 10101

Note: This query can be written in a much simpler manner. The formulation above is
simply to illustrate SQL features. Practice by writing this query in a simpler form.

SELECT COUNT(DISTINCT id)
FROM takes
WHERE (course_id, sec_id, semester, year) IN

(SELECT course_id, sec_id, semester, year
FROM teaches
WHERE teaches.ID = ‘10101’);

Classwork: Subqueries in the FROM Clause

47

● SQL allows a subquery expression to be used in the FROM clause

● Find the average instructors’ salaries of those departments where
the average salary is greater than $42,000.”:

Note: We do not need to use the HAVING clause

SELECT dept_name, avg_salary
FROM (SELECT dept_name, AVG(salary) AS avg_salary

FROM instructor
GROUP BY dept_name) AS FOO

WHERE avg_salary > 42000;

Subqueries in the FROM Clause (cont.)

48

Another way to write the same query:

SELECT dept_name, avg_salary
FROM (SELECT dept_name, AVG(salary)

FROM instructor
GROUP BY dept_name) AS dept_avg (dept_name, avg_salary)

WHERE avg_salary > 42000;

Scalar Subqueries

49

● Scalar subquery is one which is used where a single value is
expected

● List all departments along with the number of instructors in each
department

● Runtime error if subquery returns more than one result tuple

SELECT dept_name,
(SELECT COUNT(*)
FROM instructor
WHERE department.dept_name = instructor.dept_name)
AS num_instructors

FROM department;

Database Modification

50

● Deletion of tuples from a given relation.
● Insertion of new tuples into a given relation
● Updating of values in some tuples in a given relation

Deletion

51

● Delete all instructors

● Delete all instructors from the Finance department

● Delete all tuples in the instructor relation for those instructors
associated with a department located in the Watson building.

DELETE FROM instructor;

DELETE FROM instructor
WHERE dept_name = ‘Finance’;

DELETE FROM instructor
WHERE dept_name IN (SELECT dept_name

FROM department
WHERE building = ‘Watson’);

Classwork: Deletion (cont.)

52

● Delete all instructors whose salary is less than the average salary of
instructors

Note: As we delete tuples from deposit, the average salary changes
Solution used in SQL:
1. First, compute avg (salary) and find all tuples to delete
2. Next, delete all tuples found above (without recomputing avg or

retesting the tuples)

DELETE FROM instructor
WHERE salary < (SELECT AVG(salary) FROM instructor); Insertion 53 ● Add a new tuple to the course table: ● Equivalent and safer: ● Add a new tuple to student with tot_creds set to NULL INSERT INTO course VALUES ('CS-437', 'Database Systems', 'Comp. Sci.', 4); INSERT INTO course (course_id, title, dept_name, credits) VALUES ('CS-437', 'Database Systems', 'Comp. Sci.', 4); INSERT INTO student VALUES ('3003', 'Green', 'Finance', NULL); Classwork: Insertion (cont.) 54 ● Add all instructors to the student table with tot_creds set to 0: ● The SELECT-FROM-WHERE statement is evaluated fully before any of its results are inserted into the relation. Otherwise queries like: would cause problems. INSERT INTO student SELECT (id, name, dept_name, 0) FROM instructor; INSERT INTO table1 SELECT * FROM table1; Updates 55 Increase salaries of instructors whose salary is over $100,000 by 3%, and all others by a 5%: ● Write two update statements: ● The order is important ● Can be done better using the CASE statement UPDATE instructor SET salary = salary * 1.03 WHERE salary > 100000;

UPDATE instructor
SET salary = salary * 1.05
WHERE salary <= 100000; Case Statement for Conditional Updates 56 Same query as before but with CASE statement: UPDATE instructor SET salary = CASE WHEN salary <= 100000 THEN salary * 1.05 ELSE salary * 1.03 END; 50 Updates with Scalar Subqueries Recompute and update tot_creds value for all students Sets tot_creds to NULL for students who have not taken anycourse Instead of SUM(credits), use: UPDATE student S SET tot_cred = (SELECT SUM(credits) FROM takes, course WHERE takes.course_id = course.course_id AND S.ID = takes.ID AND takes.grade <> ‘F’
AND takes.grade IS NOT NULL);

CASE
WHEN SUM(credits) IS NOT NULL
THEN SUM(credits)
ELSE 0

END;

51

Acknowledgements

● Some content adapted or modified from the course assigned
textbook and its relevant resources by:
Silberschatz, A., Korth, H. F., and Sudarshan, S. (2019). Database
System Concepts, 7th Edition. McGraw-Hill.

Before we Begin…
SQL Components (review)
SQL Components (review)
Data and Domain Types: Why?
Basic Domain Types (Data Types)
Basic Domain Types (Data Types) (cont.)
Create Table Construct
Classwork-1: Create Table Construct
Integrity Constraints in CREATE TABLE
Classwork: Define Integrity Constraints in CREATE TABLE
Practice: And a Few More Table Definitions
Practice: And a Few More Table Definitions (cont.)
Classwork: Updates to the table instructors
Updates to Tables (cont.)
Classwork2: Updates to Tables (cont.)
Basic Query Structure
The SELECT Clause
The SELECT Clause (cont.)
The SELECT Clause (cont.)
The SELECT Clause (cont.)
The SELECT Clause (cont.)
The WHERE Clause
Class Work-3: WHERE Clause
The FROM Clause
Classwork: Examples
Challenge Question:
The Rename Operation
Class Work-4
Classwork Answers
Challenge Question 2?
String Operations
String Operations (cont.)
Ordering the Display of Tuples
Classwork: WHERE Clause Predicates
Null Values
Null Values and Three Valued Logic (true, false, unknown)
Aggregate Functions
Classwork: Aggregate Functions (cont.)
GROUP BY
HAVING
Null Values and Aggregates
Nested Subqueries
Classwork: Set Membership
Classwork: Set Membership (cont.)
Classwork: Set Membership (cont.)
Classwork: Subqueries in the FROM Clause
Subqueries in the FROM Clause (cont.)
Scalar Subqueries
Database Modification
Deletion
Classwork: Deletion (cont.)
Insertion
Classwork: Insertion (cont.)
Updates
Case Statement for Conditional Updates
Updates with Scalar Subqueries
Acknowledgements