3 actions on tables · web viewles01 -- select – review but using oracle these are the commands...

31
Les01 -- SELECT – review but using Oracle These are the commands used on the course document.docx by RT-- 21 June 2022 1 of 31

Upload: phunghanh

Post on 20-Apr-2018

219 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

Les01 -- SELECT – review but using Oracle

These are the commands used on the course

document.docx by RT-- 7 May 2023 1 of 27

Page 2: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

document.docx by RT-- 7 May 2023 2 of 27

Page 3: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

2

document.docx by RT-- 7 May 2023 3 of 27

Page 4: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

SCHEMA – working with this semester

REWRITE this in a better layout for readability

document.docx by RT-- 7 May 2023 4 of 27

Page 5: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

document.docx by RT-- 7 May 2023 5 of 27

Page 6: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

3 (1-4) _ repeats DBS201 – and why?

3 ACTIONS ON TABLES  1 PROJECTION 2 SELECTION 3 JOIN Done through SELECT statement

SELECT – Retrieving data from a table

1 PROJECTION -- Retrieving specific columns of data such as ALL student names and phone numbers

2 SELECTION – Returns only rows ta meet the specific restriction such as all male students from a table of students

document.docx by RT-- 7 May 2023 6 of 27

Page 7: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

3 JOIN – Returning data from 2 different tables such as course name and the student name

More on this in a later week’s lesson

document.docx by RT-- 7 May 2023 7 of 27

Page 8: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

FORMAT of the SELECT statement

document.docx by RT-- 7 May 2023 8 of 27

Page 9: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

PROJECTION –

Use the DESCRIBE command to see the structure of the tables

OR

SELECT * FROM TABS: to see the tables

document.docx by RT-- 7 May 2023 9 of 27

Page 10: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

SELECTION --

SELECT department_id, location_idFROM departments;

Demo cut and paste for labsAlso fixed font -- courier new

document.docx by RT-- 7 May 2023 10 of 27

Page 11: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

LAYOUT is important. Bad layout will have marks deducted.

document.docx by RT-- 7 May 2023 11 of 27

Page 12: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

document.docx by RT-- 7 May 2023 12 of 27

Page 13: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

9

document.docx by RT-- 7 May 2023 13 of 27

Page 14: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

10

Note how it adds to the salary an additional 300

As in ALL languages it is important to know the order of operation when there are multiple operators

SELECT last_name, salary, salary + 300FROM employees;

PROBLEM:Show yearly salary. Raise everyone’s salary (which we will assume is monthly) by 100 dollars per month, then multiply it out by 12 to see yearly salary

document.docx by RT-- 7 May 2023 14 of 27

Page 15: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

11

Raise everyone’s salary (which we will assume is monthly) by 100 dollars then multiply it out by 12 to see yearly salary

Not a large difference for EACH employee.For the company with 20 employees that is 24,000Now if you have 1000 employees, that raise is 24 million.

Would be nice if we could total the salaries to see the results.

document.docx by RT-- 7 May 2023 15 of 27

288,100 is the result

289,200

Page 16: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

12

document.docx by RT-- 7 May 2023 16 of 27

Page 17: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

13PROBLEMMultiply the monthly salary by 12 to get yearly salary.Then multiply it by the percent to get the commission earned

SELECT last_name, salary * 12 * commission_pctFROM employees;

What Happens ….

document.docx by RT-- 7 May 2023 17 of 27

Page 18: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

SELECT last_name, salary * 12 * commission_pctFROM employees

NOTE: Column Names are not nice – need to fix it with an aliasLAST_NAME SALARY*12*COMMISSION_PCT ------------------------- ------------------------ King Kochhar De Haan Hunold Ernst Lorentz Mourgos Rajs Davies Matos Vargas Zlotkey 25200 Abel 39600 Taylor 20640 Grant 12600 Whalen Hartstein Fay Higgins Gietz

20 rows selected

document.docx by RT-- 7 May 2023 18 of 27

Page 19: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

NOTE: Alias names maximum length of 30

Try these.

SELECT last_name AS name, commission_pct as commFROM employees;

SELECT last_name AS Last Name, commission_pct as commFROM employees;-- This last one won’t work as there is a space in the alias name

document.docx by RT-- 7 May 2023 19 of 27

Page 20: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

Note that used lowercase, but the default display is UPPERCASE

Using double quotes --

document.docx by RT-- 7 May 2023 20 of 27

Page 21: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

PROBLEM:

Display last name and job id from the employees table and look at the result

SELECT last_name, job_id AS "Employees"FROM employees;

Results are spread out and title Employees ---- in wrong location ……LAST_NAME Employees ------------------------- ---------- Abel SA_REP Davies ST_CLERK De Haan AD_VP

document.docx by RT-- 7 May 2023 21 of 27

Page 22: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

Change the look with a CONCATENATE OPERATOR

STILL NOT NICE LOOKING ………. Need to improve it.

How ?????

ASIDE: There is a concatenate function available later

document.docx by RT-- 7 May 2023 22 of 27

Notice this works whereas on the iSeries it "appeared" to not work.

Page 23: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

PROBLEM:Revise previous SQL require Last name and job id with the ‘is a’ between it but nicely displayedGive it a good title

Example:Ron is a Dean

(answer on next page)

document.docx by RT-- 7 May 2023 23 of 27

NOTE:

Single quotes for literals

Double quotes for alias names

Page 24: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

PROBLEM:Revise previous SQL require Last name and job id with the ‘is a’ between it but nicely displayedGive it a good title

document.docx by RT-- 7 May 2023 24 of 27

Page 25: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

ALTERNATE – don’t recall ever seeing it used … but

SELECT last_name|| q'[ has the job: ]' || job_id FROM employees;

document.docx by RT-- 7 May 2023 25 of 27

Page 26: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

PROBLEM:Using the employees table, display all the departments in HR

SELECT department_idFROM employees;

SELECT DISTINCT department_idFROM employees;

DEPARTMENT_ID ------------- 10 20 50 60 80 90 110

8 rows selected.

PROBLEM:Show the distinct salary and job id within department. Meaning if to people inb department 20 have the same salary and job_id only show one of them.

SELECT DISTINCT (salary, job_id), department_idFROM employees

ERROR see message … doesn’t look like the error is correct

WHY?

If you use DISTINCT, it must be for ALL selected columns

document.docx by RT-- 7 May 2023 26 of 27

See the duplicates as there are 20 rows displayed

Page 27: 3 ACTIONS ON TABLES · Web viewLes01 -- SELECT – review but using Oracle These are the commands used on the course 2 SCHEMA – working with this semester REWRITE this in a better

SQLPLUS command and NOT SQL

document.docx by RT-- 7 May 2023 27 of 27