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.

Slides:



Advertisements
Similar presentations
WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.
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.
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.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
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.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Database Systems and Design
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
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.
Ceng 356-Lab1. Objectives After completing this lesson, you should be able to do the following: Get Familiar with the development environment List the.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
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.
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.
SQL (DDL & DML Commands)
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
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 [ ],...]
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
Oracle 11g DATABASE DEVELOPMENT LAB2. Chapter- 2  These commands, which could be issued from SQL*Plus or SQL Developer,  will make it possible to log.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
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.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Writing Basic SQL SELECT Statements Lecture
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.
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.
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.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
Restricting and Sorting Data
Retrieving Data Using the SQL SELECT Statement
Writing Basic SQL SELECT Statements
Basic select statement
ATS Application Programming: Java Programming
Restricting and Sorting Data
Using the Set Operators
Using Subqueries to Solve Queries
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Using the Set Operators
Restricting and Sorting Data
Using Subqueries to Solve Queries
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Using CASE Value expression
Restricting and Sorting Data
Using the Set Operators
Restricting and Sorting Data
Presentation transcript:

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 90.  The rows with a value of 90 in the DEPARTMENT_ID column are the only ones returned.  This method of restriction is the basis of the WHERE clause in SQL.

 Syntax SELECT [*, DISTINCT] columns names FROM table [WHERE condition(s)];  You can restrict the rows returned from the query by using the WHERE clause.  A WHERE clause:  contains a condition that must be met  It directly follows the FROM clause.  If the condition is true, the row meeting the condition is returned.

SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90; SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90;

Before starting Character String and dates Aliases Conditions Comparison Conditions Logical Conditions Rules of Precedence

 Character strings and date values are enclosed in single quotation marks.  Character values are case sensitive (see Example 2), and date values are format sensitive.  The default date format is DD-MON-YY.

 Q: Return the last name, job id, and the department number for the employee whose last name is Goyal SELECT last_name, job_id, department_id FROM employees WHERE last_name = 'Goyal '; SELECT last_name, job_id, department_id FROM employees WHERE last_name = ‘goyal '; SELECT last_name, job_id, department_id FROM employees WHERE last_name = ‘GOYAL '; X X Some Answers

 Note that: An alias cannot be used in the WHERE clause. (see Example 3)

 Q: Return the last name, job id, and the department number for the employee whose last name is Goyal SELECT last_name AS “LN”, job_id, department_id FROM employees WHERE last_name = 'Goyal '; SELECT last_name AS “LN”, job_id, department_id FROM employees WHERE LN = ‘Goyal '; X

 Comparison conditions are used in conditions that compare one expression to a value or another expression (see Example 4)  They are used in the WHERE clause Example... WHERE hire_date='01-JAN-95'... WHERE salary>= WHERE last_name='Smith‘

Operator Meaning =Equal to >Greater than <Less than >=Greater or equal to <=Less or equal to <>Not equal to

 SELECT last_name, salary FROM employees WHERE salary <= 3000; Q: Return the last_name and salary for all employees whose salaries are not grater than 3000

OperatorMeaning 1) BETWEEN …… AND …….Between two values (inclusive) 2) IN(set)Match any of a list of values 3)LIKEMatch a character pattern 4)IS NULLIs a null value

 Use the BETWEEN condition to display rows based on a range of values.  Values specified with the BETWEEN condition are inclusive (i.e. lower limit and higher limit are included too)  You must specify the lower limit first.(see Example 5)

Q: Return the last_name and salary for all employees whose salaries are in the range 2500 and 3500

 Use the IN condition to display rows based on a specific of values. (See Example 6)

SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN (100, 101, 201); Q: Return the employee_id, last_name, salary, and manager_d for all employees whose manager_id’s is either 100,101,or 201

 The IN condition can be used with any data type. The following example returns a row from the EMPLOYEES table for any employee whose last name is included in the list of names in the WHERE clause: SELECT employee_id, manager_id, department_id FROM employees WHERE last_name IN ('Hartstein', 'Vargas');

 With the Where clause, you may not always know the exact value to search for   You can select rows that match a character pattern by using LIKE condition.  Search conditions can contain either literal characters or numbers (see Example7 & 8):  % represents any zero or many characters.  _ represent any single character.

Q:Return the first name from the EMPLOYEES table for any employee whose first name begins with an S. SELECT first_name FROM employees WHERE first_name LIKE 'S%';  Note that: Names beginning with an s (Lower case) are not returned.

 You can combine pattern-matching characters. SELECT last_name FROM employees WHERE last_name LIKE '_o%‘ ;  The previews example displays the names of all employees whose last name has an o as the second character

 In the where clause we can test for NULL using the IS NULL operator (see Example 9)

Q:Return the last name and manager_id from the EMPLOYEES table for any employee who have NO manager

operatorMeaning And Returns TRUE if both component are true Or Returns TRUE if either component are true Not Returns TRUE if the following condition is false

 The following table shows the results of combining two expressions with AND: AndtruefalseNull True FalseNull False null falsenull

 The following table shows the results of combining two expressions with OR: OrtruefalseNull True trueTrue False trueFalsenull truenull

The following table show the result of applying the not operator on a condition: nottruefalsenull falsetruenull

 Note that: The NOT operator can also be used with other SQL operators, such as BETWEEN, LIKE, and NULL. For Example ... WHERE job_id NOT IN ('AC_ACCOUNT', 'AD_VP') ... WHERE salary NOT BETWEEN AND ... WHERE last_name NOT LIKE '%A%' ... WHERE commission_pct IS NOT NULL