After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query.

Slides:



Advertisements
Similar presentations
2 Restricting and Sorting Data Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Advertisements

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.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
Database Systems and Design
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.
After completing this lesson, you should be able to do the following: Describe various types of functions available in MySQL Use character, number, and.
Subqueries.
Subqueries.
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. I Introduction.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
SQL. Relating Multiple Tables Relational Database Terminology Row PK Column FK Field NULL.
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.
Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
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.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
SQL: Part 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
Review SQL Advanced. Capabilities of SQL SELECT Statements Selection Projection Table 1 Table 2 Table 1 Join.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Oracle CONNECT BY function JAVA WEB Programming. Emp 테이블의 내용 ( 상 / 하급자 계층구조 ) SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints.
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.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.
Defining a Column Alias
Copyright  Oracle Corporation, All rights reserved. Introduction.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
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.
Restricting and Sorting Data
Enhanced Guide to Oracle 10g
Writing Basic SQL SELECT Statements
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Restricting and Sorting Data
Restricting and Sorting Data
Restricting and Sorting Data
Structured Query Language (SQL)
Restricting and Sorting Data
Subqueries Schedule: Timing Topic 25 minutes Lecture
Review SQL Advanced.
Restricting and Sorting Data
Subqueries Schedule: Timing Topic 25 minutes Lecture
Restricting and Sorting Data
Presentation transcript:

After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query

"…retrieve all employees in department 10" Employee employee_nbr name job... dept_nbr 7839 King President Blake Manager Clark Manager Jones Manager Employee employee_nbr name job... dept_nbr 7839 King President Clark Manager Miller Manager 10...

Restrict the rows returned by using the WHERE clause. The WHERE clause follows the FROM clause. SELECT[DISTINCT] {*| column [alias],...} FROM table [WHEREcondition(s)];

MySQL>SELECT name, job, dept_nbr 2 FROM employee 3 WHERE job=‘Clerk’; name job dept_nbr James Clerk 30 Smith Clerk 20 Adams Clerk 20 Miller Clerk 10

Character strings and date values are enclosed in single quotation marks. Character values are case sensitive and date values are format sensitive. The default date format is YYYY-MM-DD. MySQL>SELECTname, job, dept_nbr 2 FROM employee 3 WHEREname = ; MySQL>SELECTname, job, dept_nbr 2 FROM employee 3 WHEREname = ; ‘James’

OperatorMeaning =Equal to >Greater than >=Greater than or equal to <Less than <=Less than or equal to <>Not equal to !=Not equal to

MySQL>SELECT name, salary, commission 2 FROM employee 3 WHERE salary <= commission; name salary commission Martin

OperatorMeaning BETWEEN …AND… Between two values (inclusive) IN (list)Match any of a list of values LIKEMatch a character pattern IS NULLIs a null value

Use the BETWEEN operator to display rows based on a range of values. name salary Martin 1250 Turner 1500 Ward 1250 Adams 1100 Miller 1300 MySQL>SELECTname, salary 2 FROM employee 3 WHEREsalary BETWEEN 1000 AND 1500; Lower limit Higher limit

Use the IN operator to test for values in a list. MySQL>SELECTemployee_nbr, name, salary, manager 2 FROM employee 3 WHEREmanager IN (7902, 7566, 7788); employee_nbr name salary manager FORD SMITH SCOTT ADAMS

MySQL>SELECTname 2 FROM employee 3 WHEREname LIKE 'S%'; Use the LIKE operator to perform wildcard searches of valid search string values. Search conditions can contain either literal characters or numbers. % denotes zero or many characters. _ denotes one character. Searches are case sensitive.

You can combine pattern-matching characters. You can use the ESCAPE identifier to search for "%" or "_". MySQL>SELECTname 2 FROMemployee 3 WHEREname LIKE ‘_a%’; name Martin James Ward

Test for null values with the IS NULL operator. MySQL>SELECT name, manager 2 FROM employee 3 WHERE manager IS NULL; name manager King

OperatorMeaning AND Returns TRUE if both component conditions are TRUE OR Returns TRUE if either component conditions is TRUE NOT Returns TRUE if the following condition is FALSE

MySQL>SELECT employee_nbr, name, job, salary 2 FROM employee 3 WHERE salary >= AND job = 'Clerk'; employee_nbr name job salary Adams Clerk Miller Clerk 1300 AND requires both conditions to be TRUE.

MySQL>SELECT employee_nbr, name, job, salary 2 FROM employee 3 WHERE salary >= OR job = ‘Clerk’; employee_nbr name job salary King President Blake Manager Clark Manager Jones Manager Martin Salesman James Clerk rows selected. OR requires either conditions to be TRUE.

MySQL>SELECT name, job 2 FROM employee 3 WHERE job NOT IN ('Clerk','Manager',’Analyst'); name job King President Martin Salesman Allen Salesman Turner Salesman Ward Salesman

Override rules of precedence by using parentheses. Order EvaluatedOperator 1All comparison operators 2NOT 3AND 4OR

name job Salary King President 5000 Martin Salesman 1250 Allen Salesman 1600 Turner Salesman 1500 Ward Salesman 1250 name job Salary King President 5000 Martin Salesman 1250 Allen Salesman 1600 Turner Salesman 1500 Ward Salesman 1250 MySQL>SELECT name, job, salary 2 FROM employee 3 WHERE job = ‘Salesman’ 4 OR job = ‘President’ 5 AND salary > 1500;

name job salary King President 5000 Allen Salesman 1600 name job salary King President 5000 Allen Salesman 1600 MySQL>SELECT name, job, salary 2 FROM employee 3 WHERE (job = ‘Salesman’ 4 OR job = ‘President’) 5 AND salary > 1500; Use parentheses to force priority.

Sort rows with the ORDER BY clause ASC: ascending order, default DESC: descending order The ORDER BY clause comes last in the SELECT statement. MySQL>SELECT name, job, dept_nbr, hire_date 2 FROM employee 3 ORDER BY hire_date; name job dept_nbr hire_date Smith Clerk Allen Salesman rows selected.

MySQL>SELECT name, job, dept_nbr, hire_date 2 FROM employee 3 ORDER BY hire_date DESC; name job dept_nbr hire_date Adams Clerk Scott Analyst Miller Clerk James Clerk Ford Analyst King President Martin Salesman rows selected.

MySQL>SELECT employee_nbr, name, salary*12 annual_salary 2 FROM employee 3 ORDER BY annual_salary; employee_nbr name annual_salary SMITH JAMES ADAMS MARTIN WARD Miller TURNER rows selected.

The order of ORDER BY list is the order of sort. MySQL>SELECT name, dept_nbr, salary 2 FROM employee 3 ORDER BY dept_nbr, salary DESC; name dept_nbr Salary King Clark Miller Ford rows selected. You can sort by a column that is not in the SELECT list.

SELECT[DISTINCT] {*| column [alias],...} FROM table [WHEREcondition(s)] [ORDER BY{column, expr, alias} [ASC|DESC]];