290 likes | 393 Views
Chapter Zero. Objectives: Review Introduction to SQL. SQL Name:. User Identifiers: 1-30 characters Start with an alphabet Followed by alphabet, digit, _ Unique Not reserved Not case sensitive. Data Types:. Oracle Data Types: CHAR(size) VARCHAR2(max_size) NUMBER(n,d) DATE.
E N D
Chapter Zero Objectives: Review Introduction to SQL
SQL Name: User Identifiers: 1-30 characters Start with an alphabet Followed by alphabet, digit, _ Unique Not reserved Not case sensitive
Data Types: Oracle Data Types: • CHAR(size) • VARCHAR2(max_size) • NUMBER(n,d) • DATE
Display a Structure of a Table: DESCRIBE Students; Name Null? Type ---------------------------------------------------------- FNAME VARCHAR2(40) • LNAME VARCHAR2(40) ID NUMBER(9) • GPA NUMBER(3,2) • B_Date DATE • MAJOR CHAR(4) • MINOR CHAR(4)
General Format SELECT fieldnames FROM relation [ WHERE condition] [ GROUP BY group_field ] [ HAVING condition] [ ORDER BY fieldname] ;
SELECT: SELECT * FROM students ; SELECT lname , major, gpe FROM students ;
SELECT: SELECT salary , name , salary/12 Monthly_Salary FROM faculty; SELECT name , salary/12 AS Monthly_Salary FROM faculty; SELECT name , salary/12 “Monthly Salary” FROM faculty;
Column Format */ Example of column format*/ COLumn salary FORMAT 999999.99; COLumn salary FORMAT 999,999.99; COLumn salary FORMAT $999,999.99; SELECT salary FROM employee;
Column Format SELECT salary FROM employee; COLUMN name FORMAT A5; COLUMN name FORMAT A5 WRApped; COLUMN name FORMAT A5 TRUncated; COLUMN salary/12 ALIAS m_salry; COLUMN m_salry FORMAT 99,999.99; CLEAR COLUMNS;
SELECT: SELECT DISTINCT major FROM students; SELECT f_mame , l_name , major FROM students; SELECT f_mame || l_name , major FROM students; /* what is the output? /*
SELECT: SELECT f_mame || ‘ ‘ || l_name , gpa FROM students; SELECT f_mame || ‘ ‘ || l_name , gpa FROM students WHERE major=‘COSC’;
SELECT: SELECT f_mame || ‘ ‘ || l_name , gpa FROM students WHERE major = ‘COSC’ OR major = ‘MATH’; SELECT f_mame || ‘ ‘ || l_name , gpa FROM students WHERE major = ‘COSC’ AND minor = ‘MATH’ ;
SELECT: SELECT f_mame || ‘ ‘ || l_name , gpa FROM students WHERE major IS NULL; SELECT l_mame , gpa FROM students WHERE major IS NOT NULL;
SELECT: SELECT name, NVL(major, ‘unknown’) FROM student; SELECT name, NVL(GPA, 0.0) FROM student;
SELECT: SELECT name , salary , dept FROM faculty WHERE salary BETWEEN 20000 AND 50000; SELECT name , salary , dept FROM faculty WHERE salary NOTBETWEEN 20000 AND 50000;
SELECT: SELECT name , salary , dept FROM faculty WHERE name BETWEEN ‘L’ AND ‘O’;
SELECT: SELECT name FROM Student WHERE major IN (‘COSC’, ‘MATH’); SELECT name FROM Student WHERE id NOT IN (1111 , 4322 , 4567 , 6543);
SELECT: SELECT * FROM students WHERE lname LIKE ‘_ED’; SELECT lname , major FROM students WHERE lname LIKE ‘%ED’;
SINGLE ROW FUNCTION: CHARACTER FUNCTION • LOWER(‘ITEC 454 Courses’) • UPPER (‘itec 454 Courses’) • INTCAP (‘itec 454 Courses’) • LENGTH (‘itec 454 Courses’) • INSTR (‘itec 454 Courses’, ‘C’) • LTRIM (‘itec 454 Courses’, ‘itec’) • RTRIM (‘itec 454 Courses’, ‘i') • SUBSTR (‘itec 454 Courses’, 6, 3) • LPAD (‘itec 454 Courses’, ‘--->’) • RPAD (‘itec 454 Courses’, ‘<---’)
SINGLE ROW FUNCTION: NUMBER FUNCTION • ROUND(123.163, 2) • ROUND(123.163, -2) • TRUNC(123.163, 2) • MOD(3093) • POWER(2, 3)
DATE FUNCTION • MONTH_BETWEEN(SYSDATE, b_date) • ADD_MONTHS(b_date, 3) • ROUND(b_date, ‘MONTH’) • ROUND(b_date, ‘YEAR’)
CONVERT DATATYPE FUNCTION • TO_CHAR(b_date, ‘DD-MM-YY’) • FORMAT: • YY • YYY • YYYY • YEAR • MM • MONTH • DD • DY
CONVERT DATATYPE FUNCTION • FORMAT • DAY • MI • SS • HH
AGGREGATE FUNCTION • MAX • MIN • SUM • COUNT • DISTINCT
ORDERING • SELECT name, gpaFROM studentsWHERE major=‘COSC’ORDER BY name • ASC • DESC
GROUPING • SELECT dept, AVG(salary)FROM facultyGROUP BY dept; • SELECT major, COUNT(*)FROM studentWHERE major IN(‘COSC’, ‘MATH’, ‘VART’, ‘ACCT’, ‘ITEC’)GROUP BY majorHAVING COUNT(*)>20ORDER BY major;
JOIN • SELECT nameFROM students, student_courseWHERE students.id=student_course.id; • SELECT *FROM students a, students bWHERE a.gpa > b.gpa AND a.lname = ‘SMITH’;
JOIN • SELECT name, salaryFROM faculty, statusWHERE salary BETWEEN low_salary AND high_salary;
OUTER JOIN • SELECT nameFROM students, student_courseWHERE student.id = student_course.id(+);