Lecture 2 21/1/16
Putting the output in order Oracle can sort a column, or multiple columns select emp_no, emp_name from employee order by emp_name; emp_no emp_name 4BYRNE 8HARTE 3HEARNE 6WALSH select * from customer order by name; Name phone address CHARLES KERRY CIARA CORK MARY CORK
Where Select emp_name, sal, dept_no from employee Where job= 'ANALYST'; emp_name sal dept_no HEARNE8004 Select * from customer where name='CIARA'; NAME ADDRESS PHONE CIARA CORK
More… Select emp_name, sal from employee where job='CLERK'; emp_name sal BYRNE100 4
Operator Precedence select emp_name, sal, 12*(sal+100) from employee; 5
Null Values NULL values represent unavailable, unassigned, unknown or inapplicable data A NULL is not the same as a zero or a blank space. 6
Null Values Name Null Type EMP_NO NOT NULL NUMBER(4) EMP_NAME VARCHAR2(20) JOB VARCHAR2(20) DEPT_NO NUMBER(2) SAL VARCHAR2(20) COMM VARCHAR2(20) 7
Note VARCHAR2 is a data type used to store variable-length character data. A varchar2 value can contain up to 4000 bytes of data (limit in Oracle 7 was 2000 bytes). The NUMBER datatype stores fixed and floating-point numbers. Numbers of virtually any magnitude can be stored and are guaranteed portable among different systems operating Oracle Database, up to 38 digits of precision. atype.htm#CNCPT313
Null Values 3HEARNEANALYST BYRNECLERK3100 6WALSHMANAGER4350 8HARTEACCOUNTANT DOHERTYCLERK MARTINMANAGER
Null Values select COMM from employee where emp_name='DOHERTY'; 10 COMM – commission awarded to employees
Concatenation Operator Concatenation means to join, or link together In Oracle we use two bars|| This joins two, or more, columns together select EMP_NAME || ' IS A '|| JOB from EMPLOYEE; 11
12
select NAME || ' CONTACT DETAILS '|| PHONE from CUSTOMER; 13
Literal Character Strings Character, number or date that is included in a SELECT Date and character literals must be enclosed by single quotes Each string is output once for each row Example select EMP_NAME || ' IS A '|| JOB from EMPLOYEE; 14
Note A string is a sequence of bytes or characters, enclosed within either single quote (“'”) or double quote (“"”) characters. Examples: 'a string' "another string“ literals.html
Comparison Operators OperatorMeaning = (Equals)Equal to > (Greater Than)Greater than < (Less Than)Less than >= (Greater Than or Equal To)Greater than or equal to <= (Less Than or Equal To)Less than or equal to <> (Not Equal To)Not equal to != (Not Equal To)Not equal to (not ISO standard) !< (Not Less Than)Not less than (not ISO standard) !> (Not Greater Than)Not greater than (not ISO standard) 16
Comparison Operators… select * from customer where name ='CIARA'; select emp_name, sal from employee where sal >300 order by emp_name; select emp_name, sal from employee where sal <300 order by emp_name; select * from employee where dept_no >=4 order by emp_name; 17
What if? select * from customer where name ='ciara';
Comparison Operators select * from employee where dept_no <=4 order by emp_name; 19
Comparison Operators select * from employee where dept_no <>4; 20
More Examples select * from emp where deptno <>20; 7934MILLERCLERK JAN JAMESCLERK DEC TURNERSALESMAN SEP KINGPRESIDENT17-NOV CLARKMANAGER JUN BLAKEMANAGER MAY MARTINSALESMAN SEP WARDSALESMAN FEB ALLENSALESMAN FEB
More examples select * from emp where deptno <>20 and deptno <>10; 7900JAMESCLERK DEC TURNERSALESMAN SEP BLAKEMANAGER MAY MARTINSALESMAN SEP WARDSALESMAN FEB ALLENSALESMAN FEB
Displaying Table Structure DESC Displays the structure of the table including column names and data types 23
Datatypes The VARCHAR2 datatype stores variable-length character strings The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings. The NUMBER datatype stores fixed and floating-point numbers. Numbers of virtually any magnitude can be stored and are guaranteed portable among different systems operating Oracle Database, up to 38 digits of precision The following numbers can be stored in a NUMBER column: Positive numbers in the range 1 x to x with up to 38 significant digits Negative numbers from -1 x to x with up to 38 significant digits Zero Positive and negative infinity 24
Datatypes – Number continued… For numeric columns, you can specify the column as: column_name NUMBER Optionally, you can also specify a precision (total number of digits) and scale (number of digits to the right of the decimal point): column_name NUMBER (precision, scale) 25
Datatypes The DATE datatype stores point-in-time values (dates and times) in a table. The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight) Oracle Database uses its own internal format to store dates. Date data is stored in fixed-length fields of seven bytes each, corresponding to century, year, month, day, hour, minute, and second. For input and output of dates, the standard Oracle date format is DD-MON-YY, as follows: '13-NOV-92' 26
Datatypes The VARCHAR2 datatype stores variable-length character strings The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings. The NUMBER datatype stores fixed and floating-point numbers. Numbers of virtually any magnitude can be stored and are guaranteed portable among different systems operating Oracle Database, up to 38 digits of precision The following numbers can be stored in a NUMBER column: Positive numbers in the range 1 x to x with up to 38 significant digits Negative numbers from -1 x to x with up to 38 significant digits Zero Positive and negative infinity 27
Datatypes The DATE datatype stores point-in-time values (dates and times) in a table. The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight) Oracle Database uses its own internal format to store dates. Date data is stored in fixed-length fields of seven bytes each, corresponding to century, year, month, day, hour, minute, and second. For input and output of dates, the standard Oracle date format is DD-MON-YY, as follows: '13-NOV-92' 28
Commands can be divided into four categories Query. Data Manipulation Language (DML) commands Data Definition Language (DDL) commands Security 29
Entering SQL commands SQL has a buffer which stores the current command SQL commands can span lines Although not obligatory, clauses should be put on separate lines to improve readablity Tabs should be used to make a command more readable SQL commands are not case sensitive 30
Like % Select ename, job from emp where job like ‘C%'; ENAMEJOB MILLERCLERK JAMESCLERK ADAMSCLERK SMITHCLERK Select ename, job from emp where job like ‘c%'; Result??? Another example: select * from emp where job like '%NALYST'; 3HEARNEANALYST
More Examples Select ename, job from emp where job like 'CL%'; Select ename, job from emp where job like 'CL%RK'; Select ename, job from emp where job like 'CL%%'; Select ename, job from emp where job like 'CL%%'; Select ename, job from emp where job like '%%K';
Error? Select * from customer Select name address from customer; Select name, address from customer where name =mary; Select name CUS NAME from customer; 34
SQL Questions cust_codecust_namecust_city working_a rea cust_count ry grade opening_a mt receive_a mt payment_ amt outstandin g_amt phone_no agent_cod e C00013HolmesLondon UK BBBBBBBA003 C00001MichealNew York USA CCCCCCCA008 C00020AlbertNew York USA BBBBSBBA008 C00025RavindranBangalore India AVAVAVAA011 C00024CookLondon UK FSDDSDFA006 C00015StuartLondon UK GFSGERSA003 C00002BoltNew York USA DDNRDRHA008 C00018FlemingBrisban Australia NHBGVFCA005 C00021JacksBrisban Australia WERTGDFA005 C00019 Yearannaid u Chennai India ZZZZBFVA Create a report to list: 1.All customer names in alphabetical order 2.Customer names and phone numbers of those living in London 3.Customer countries – avoid duplicates 4.Customer names and codes where their outstanding amount is less than Customer countries with ‘U’ in the name 6.All customer details where the sum of 'opening_amt' and 'receive_amt' is greater than 15000