0% found this document useful (0 votes)
64 views

RestrictingAndSortingData SQL

This document provides information on restricting and sorting data in SQL queries. It discusses using the WHERE clause to filter rows, comparison operators like = and BETWEEN, pattern matching with LIKE, NULL conditions, AND, OR and NOT operators, and ORDER BY to sort rows. It also covers substitution variables that allow values to be passed into a SQL statement at runtime for dynamic filtering and sorting.

Uploaded by

Wendy Fragoso
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

RestrictingAndSortingData SQL

This document provides information on restricting and sorting data in SQL queries. It discusses using the WHERE clause to filter rows, comparison operators like = and BETWEEN, pattern matching with LIKE, NULL conditions, AND, OR and NOT operators, and ORDER BY to sort rows. It also covers substitution variables that allow values to be passed into a SQL statement at runtime for dynamic filtering and sorting.

Uploaded by

Wendy Fragoso
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

********************************************************************************

********************************************
**
**
** /* RESTRICTI
NG AND SORTING DATA */
**
**
**
********************************************************************************
********************************************
Limit the rows that are retrieved by a query
Sort the rows that are retrieved by a query
Use ampersand substitution to restrict and sort output at run time
-------------------------------------------------- LIMITING ROWS ---------------
--------------------------------------------
---------------------------------------------------------
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];
----------------------------------------------------------
WHERE
condition --> Composed fo COLUMN NAMES, EXPRESSIONS,CONSTANTS, COMPARISON OPERAT
OR
--> return a value TRUE, FALSE or UNKNOWN

-----------------Using the WHERE clause
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE department_id = 90;

--------------------CHARACTERS STRINGS AND DATES
--strings and dates enclosed with quotation marks
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = Whalen; -->Characters are case sensitive and data values are
format sensitive
--'WHALEN' not return any row because es case-semsitive

---The default day display format is DD-MON-RR
SELECT last_name, hire_date
FROM employees
WHERE hire_date='17-FEB-96'; -->DD-MON-RR
-->OUTPUT: Hartstein 17/02/96
SELECT last_name, hire_date
FROM employees
WHERE hire_date='17/FEB/96'; --> also this is executed succesfully

-----------------------COMPARISON OPERATORS

=
>
>=
<
<=
BETWEEN ....AND... --Between two values(inclusive) BETWEEN 2500 AND 3500
IN (set) --Match any of a list of values
LIKE --Match a character pattern
IS NULL --Is a null value
!= ---NOT EQUAL TO
^= ---NOT EQUAL TO
<> --NOT EQUAL TO
NOTE: <>,!=, --> All them represent the not equal condition
SELECT last_name, salary
FROM employees
WHERE salary <= 3000; ---Comparison Operators

SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500; ---Range COnditions Usin BETWEEN

-->You can also use with character values
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 'King' AND 'Smith'; ---Range COnditions with character va
lues

--> IN operator is internally evaluated by ORacle as a set of OR conditions
SELECT employee_id, last_name, salary, manager_id
FROM employees
WHERE manager_id IN (100,101,201); ---Membership Condition Usin IN Operator

-->NOTE: THE "IN" OPERATOS CAN BE USE WITH ANY DATA TYPE
SELECT employee_id, last_name, salary, manager_id
FROM employees
WHERE manager_id IN ('Harstein','Vargas'); ---Use with any data type

SELECT first_name
FROM employees
WHERE first_name LIKE 'S%'; ---> Pattern Matching Using LIKE Operator ---COIN
CIDENCIA DE PATRONES

% -->Denotes zero or many characters
_ -->Denotes one character

---You can combine the two wildcard characters (%,_) with literal characters for
pattern matching

----------------ESCAPER IDENTIFIER

SELECT employee_id, last_name, job_id
FROM employees WHERE job_id LIKE '%SA\_%' ESCAPE '\'; '-->For have a exact ma
tch for the actual % or _ specifie what the
--escape character is
WHERE job_id LIKE '%SA*_%' ESCAPE '*'; --> With "*" like escape character
----------------------USING the NULL CONDITIONS

SELECT first_name, last_name, commission_pct
FROM employees
WHERE commission_pct IS NULL; ---> NULL values is UNAVAILABLE, UNASSIGNED, UN
KNOWN, INAPPLICABLE
--ALSO
WHERE commission_pct IS NOT NULL;
--------------------------------
SELECT first_name, last_name, commission_pct
FROM employees
WHERE commission_pct = NULL; --> This is executed successfully but don't retu
rn any value
-->NULL cannot be equal or unequal to any value
--------------------------------
WHERE commission_pct IN NULL; -->Executed but don't return any value


---------------------USING AND OPERATOR
TRUE TRUE = TRUE
TRUE FALSE = FALSE
TRUE NULL = NULL
FALSE TRUE = FALSE
FALSE FALSE = FALSE
FALSE NULL = FALSE
NULL TRUE = NULL
NULL FALSE = FALSE
NULL NULL = NULL
----------------------USING OR OPERATOR
TRUE TRUE = TRUE
TRUE FALSE = TRUE
TRUE NULL = TRUE
FALSE TRUE = TRUE
FALSE FALSE = FALSE
FALSE NULL = NULL
NULL TRUE = TRUE
NULL FALSE = NULL
NULL NULL = NULL
-----------------------USING NOT OPERATOR

TRUE = FALSE
FALSE = TRUE
NULL = NULL
NOTE: NOT can also be used with other SQL operators

SELECT * FROM employees
WHERE job_id NOT IN ('AC_ACCOUNT', 'AD_VP')

NOT BETWEEN 1000 and 1500
NOT LIKE '%A%'
IS NOT NULL


------------------------------------RULES OF PRECEDENCE FOR OPERATORS IN AN EXPR
ESSION --------------------------------------
1) Arithmetic operators
2) Concatenation operator
3) Comparison Conditions
4) IS [NOT]NULL, LIKE, [NOT] IN
5) NOT BETWEEN
6) NOT equal to
7) NOT logical condition
8) AND logical condition
9) OR logical condition
------------------------------------SORTING ROWS USING ORDER BY ----------------
---------------------------------------------
Can be used for sort the rows
NOTE: it must be the last clase of the SQL statement
ASC --> Ascending order default
DESC --> Descending order
SYNTAX
------------------------------------------------------------
SELECT expr
FROM table
[WHERE condition(s)]
[ORDER BY {column, expr, numeric_position} [ASC|DESC]]; --> The defaul order is
ascending and you can also sort by a column that is not in the SELECT
-------------------------------------------------------------
NOTE: USe the keyword NULLS FIRST or NULLS LAST to specify wheter returned rows
containing null values should
appear first or last in the ordering sequence
SELECT *
FROM employees
ORDER BY 9 DESC nulls first;
---Sorting in descending order
SELECT last_name, job_id, hire_date
FROM employees
ORDER BY 3 DESC;

---Sorting by column alias
SELECT last_name AS apellido --->Using the alias apellido
FROM employees
ORDER BY apellido;

---Sorting by using the columns numeric position
SELECT last_name AS apellido
FROM employees
ORDER BY 1;

---Sorting by multiple columns
SELECT last_name, job_id, hire_date
FROM employees
ORDER BY last_name, 3 DESC; --->Sort by multiple columns and specify the order
for the second column
-------------------------------------------SUBSTITUTION VARIABLES --------------
---------------------------------------------
(&) --> Use to temporarily store values with single-ampersand and the double
(&&)
--> Use to supplement the following
WHERE
ORDER BY
Column expressions -->select 2*&m FROM &tabla;
Table names
Entire SELECT statement
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num; --(&) with the single ampersand the user is
prompted every time the command is executed is the variable does not exist
-----------------------CHARACTER AND DATE VALUES WITH SUBSTITUTION VARIABLES---
------------
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE job_id = &job_title; --Need character so need single quation marks also
is need for date values

-->OUTPUT:
ORA-00904: "IT_PROG": identificador no vlido
00904. 00000 - "%s: invalid identifier"

WHERE job_id = '&job_title'; -->In this way is executed successfully


NOTE: A subtitution variable can be used anywhere in the SELECT statement, excep
t as the first word
entered at the command prompt
(&&) use double ampersand if you want to reuse the variable value without prompt
ing the user each time
SELECT employee_id, last_name, salary, &&department_id ---> just prompt one time
and SQL Developer stores the value using DEFINE command
FROM employees
ORDER BY &column_name;

UNDEFINE department_id -->USe this command is you want to prompt the user agai
n UNDEFINE column_name
----------------------------------------------DEFINE AND VERIFY ----------------
--------------------------------------------
DEFINE/UNDEFINE commands
SUBSTITUTION VARIABLE support by SQL Developer and SQL*PLus
DEFINE-->for predifine variables, create and assigns a value to variable
DEFINE --> to create and assign a value to a variable
UNDEFINE --> to remove a variable
DEFINE employee_num=200
SELECT employee_id, last_name, salary,department_id
FROM employees
WHERE employee_id = &employee_num;
UNDEFINE employee_num
VERIFY ---> to toggle the display of the substitution variable, both before and
after SQL Developer
--replace substitution variables with values
SET VERIFY ON
SET VERIFY OFF
SHOW ALL ---> for see ALL THE SYSTEM VARIABELS
********************************************************************************
********************************************
**
**
** /* PRAC
TICE 2: OVERVIEW */
**
**
**
********************************************************************************
********************************************
--- 13)
Desplegar el apellido de todos los empleados que tengan "a" y e en su apellido
SELECT last_name
FROM employees
WHERE last_name LIKE '%a%'
AND last_name LIKE '%e%';
--- 14)
Desplegar el apellido, job_id y salario de todos los empleados que su job_id sea
SA_REP(sales representative) y ST_CLERK(stock clerk)
y su salario no igual a $2500, $3500 y $7000
SELECT last_name, job_id, salary
FROM employees
WHERE salary NOT IN (2500,3500,7000)
AND job_id IN ('SA_REP','ST_CLERK');


--- 1)
SELECT last_name, salary
FROM employees
WHERE salary > 12000;
----2)
SELECT last_name, department_id
FROM employees
WHERE employee_id = 176;

--- 3)
SELECT last_name, salary
FROM employees
WHERE salary NOT BETWEEN 5000 AND 12000;

--- 4)
SELECT last_name, job_id, hire_date
FROM employees
WHERE last_name IN ('MAtos','Taylor')
ORDER BY hire_Date;
--- 5)
SELECT last_name, job_id, hire_date
FROM employees
WHERE department_id in (20,50)
ORDER by last_name ASC;

--- 6)
SELECT last_name Employee, salary "Monthly Salary"
FROM employees
WHERE salary BETWEEN 5000 AND 12000
AND department_id in (20,50);

---- 7)
SELECT last_name, hire_date
FROM employees
WHERE hire_date LIKE '%94';
--- 8)
SELECT last_name, job_id
FROM employees
WHERE manager_id IS NULL;
--- 9)
SELECT last_name, salary, commission_pct
FROM employees
WHERE commission_pct IS NOT NULL
ORDER BY 2 DESC, 3 DESC;

--- 10)
SELECT last_name, salary
FROM employees
WHERE salary>&sueldo;

--- 11)
SELECT employee_id,last_name, salary, department_id
FROM employees
WHERE manager_id = &manager
ORDER BY &columna;

--- 12)
SELECT last_name
FROM employees
WHERE last_name LIKE '__a%';
--- 13)
SELECT last_name
FROM employees
WHERE last_name LIKE '%a%'
AND last_name LIKE '%e%';
--- 14)
SELECT last_name, job_id, salary
FROM employees
WHERE salary NOT IN (2500,3500,7000)
AND job_id IN ('SA_REP','ST_CLERK');
--- 15)
SELECT last_name Employee, salary "Monthly Salary", commission_pct
FROM employees
WHERE commission_pct = .20;

You might also like