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;

Slides:



Advertisements
Similar presentations
Virtual training week 4 structured query language (SQL)
Advertisements

2 Restricting and Sorting Data Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Chapter 1 Writing Basic SQL Statements Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
Ceng 356-Lab1. Objectives After completing this lesson, you should be able to do the following: Get Familiar with the development environment List the.
Oracle Data Definition Language (DDL)
Structured Query Language S Q L. What is SQL It is a database programming language developed by IBM in the early 1970’s. It is used for managing and retrieving.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Single-Row Functions. SQL Functions Functions are a very powerful feature of SQL and can be used to do the following: Perform calculations on data Modify.
1 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.
SQL (DDL & DML Commands)
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
IT The Relational DBMS Section 07. Relational Database Theory Using a Relational DBMS.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
SQL FUNDAMENTALS SQL ( Structured Query Language )
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
Academic Year 2015 Autumn. MODULE CC2006NI: Data Modelling and Database Systems Academic Year 2015 Autumn.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
1 Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
RELATSIOONILISED ANDMEBAASID (algus , ORACLE materjalid)
Retrieving Data Using the SQL SELECT Statement
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
Retrieving Data Using the SQL SELECT Statement
Writing Basic SQL Statements
Retrieving Data Using the SQL SELECT Statement
Restricting and Sorting Data
Presentation transcript:

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