You are on page 1of 7

SQL

Relational Database is a collection of relations or two dimensional tables. (RDBMS) Table Name = Employees Table Name = Departments Employees is a table Employee_Id = Primary Key (#*) Unique and Not NULL Department_Id = Foreign Key Departments is a table Department_Id = Foreign Key Optional unless Not NULL Unique Identifier = # (must be different) Mandatory = * Projection = Select columns Selection = Select rows DML (Data Manipulation Language) Select Insert Update Delete Merge DDL (Data Definition Language) Create Alter Drop

Rename Truncate Commit DCL (Data Control Language) Grant Revoke Transaction Control Commit Rollback Savepoint Scheme is Tables within the database Select * from employees All columns Select Department_id from departments Specific columns Arithmetic Expressions 1. Multiplications and division done first 2. Addition and Subtract done second 3. Override with ( ) Parenthesis + Salary + 300 - Salary 300 * 12 * salary / Salary /4

Column Alias As Product Name Double Quotes Without double quotes name or name and underscore Literal Character String Character To_char function with numbers o 9 number o 0 zero to be displayed o $ floating $ sign o L uses floating local currency symbol o . Prints a decimal point o , Prints a comma Select to_char(salary, $99,999.00) Select to_char(hire_date, fmDD Month YYYY) as Hire Date from employees; Number To_number converts character string to a number format o To Date Date format defaults to DD-MON-RR o Select last_name, (sysdate-hire_date)/7 as Weeks; o Months_Between (01-Sep-94, 11-Jan-95) o Add_Months(31-Jan-96,1) Answer = 29-Feb-96 o Next_Day (01-Sep-95, Friday) Answer = 08-Sep-95 o Round(sysdate, month) 01-Aug-03 o Trunc(sysdate, month) 01-Jul-03 o Trunc(sysdate, year) 01-Jul-03

Distinct Do not want duplicate rows Select distinct department_id from employees; Table Structure Describe Desc employees; Select * from employees where employee_id = 300; Character String and Date values Single Comparison Operator = > < >= <= <>not null Is null Between 2500 and 3000 In(100, 200, 300) Like %Rep Rep% %Rep%

Null cannot be used with numbers is used a terminator that cannot see
REM --- What is the length of this? rem --- Null is used internally as a terminator rem --- so that the end of '123' there is a implicit Null that rem --- we do not SEE because the software SQL is placing it there. rem --- || is Oracle sql is used for Concatentation. rem --select length('ABCDEFG' || '123' || NULL) from dual; SELECT last_name, 12*salary*commission_pct FROM employees; SELECT last_name, 12*salary*commission_pct as "Annual Commission", salary, commission_pct FROM employees; rem --- how to fix this problem. rem --- we need to convert a null value to zero. SELECT last_name, 12* salary * NVL(commission_pct,0) as "Annual Commission", salary, NVL(commission_pct, 0) as commission_pct FROM employees; rem --- let's us make it more simple; rem --- solve NULL problem with NVL function. rem --select 1 + 0, 1 + NULL, 1 + NVL(NULL,0) from dual;

Using AND / OR Operator Select employee_id from employees where employee_id = 200 AND last_name = Fay; You can also use OR with or without AND NOT In Select * From employees Where employee_id Not In(IT_Prog, SA_Rep); ORDER BY Select * from employees order by employee_id; SORTING Select * from employees order by employee_id asc; Ascending default Descending Desc Order by columns Select employee_id, last_name, first_name, job_id from employees Order by 4;
Results

Job_id it is ordered by

Aggregate Function Count Select count(*) from employees;

Create Tables
create table friends (first_name varchar2(30), middle_name varchar2(30), last_name varchar2(30), phone_number varchar2(10), address varchar2(40), email_address varchar2(40), postal_zip varchar2(10), age NUMBER(3), sex VARCHAR2(1), - M - Male, F - Female, U-UnKnown - do cross broder date_of_birth DATE, date_of_meet DATE);

Insert
INSERT INTO FRIENDS VALUES('Trish', 'L', 'Riley', '1234567890', '33 City Centre Drive, Suite 240', 'TRISHRILEY@ROGERS.COM', 'L4C7V7', 47, 'F', '29-DEC-63', '11JUL-11');

Using Sysdate Select sysdate from dual;

Character Function