0% found this document useful (0 votes)
13 views41 pages

SQL 1 (Basic)

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 41

SQL Part 1

kal@ittelkom.ac.id

Outline
SQL History Basic SQL Select Statement Arithmetic Operators Where Clause Sorting

SQL History
The first version of SQL was developed at IBM by Andrew Richardson, Donald C. Messerly and Raymond F. Boyce in the early 1970s. This version, initially called SEQUEL, was designed to manipulate and retrieve data stored in IBM's original relational database product, System R. IBM patented their version of SQL in 1985, while the SQL language was not formally standardized until 1986 by the American National Standards Institute (ANSI) as SQL-86.

SQL Statements
A SELECT statement retrieves information from the database. Using a SELECT statement, you can do the following:
Projection: You can use the projection capability in SQL to choose the columns in a table that you want returned by your query. You can choose as few or as many columns of the table as you require. Selection: You can use the selection capability in SQL to choose the rows in a table that you want returned by a query. You can use various criteria to restrict the rows that you see. Joining: You can use the join capability in SQL to bring together data that is stored in different tables by creating a link between them. You learn more about joins in a later lesson.

Aljabar Relational Review


Projection

Selection

Table 1 Join

Table 1

Table 1

Table 2

Basic SELECT Statement


SELECT FROM *|{[DISTINCT] column|expression [alias],...} table;

SELECT identifies what columns FROM identifies which table

Selecting All Columns


SELECT * FROM departments;

Selecting Specific Columns


SELECT department_id, location_id FROM departments;

Arithmetic Expressions
Create expressions with number and date data by using arithmetic operators.
Operator + Description Add Subtract

*
/

Multiply
Divide

Using Arithmetic Operators


SELECT last_name, salary, salary + 300 FROM employees;

Operator Precedence
* / +
_

Multiplication and division take priority over addition and subtraction. Operators of the same priority are evaluated from left to right. Parentheses are used to force prioritized evaluation and to clarify statements.

Operator Precedence
SELECT last_name, salary, 12*salary+100 FROM employees;

Using Parentheses
SELECT last_name, salary, 12*(salary+100) FROM employees;

Defining a Column Alias


A column alias: Renames a column heading Is useful with calculations Immediately follows the column name there can also be the optional AS keyword between the column name and alias Requires double quotation marks if it contains spaces or special characters or is case sensitive

Using Column Aliases


SELECT last_name AS name, commission_pct comm FROM employees;

SELECT last_name "Name", salary*12 "Annual Salary" FROM employees;

Concatenation Operator
A concatenation operator: Concatenates columns or character strings to other columns Is represented by two vertical bars (||) Creates a resultant column that is a character expression

Using the Concatenation Operator


SELECT FROM last_name||job_id AS "Employees" employees;

Literal Character Strings


A literal is a character, a number, or a date included in the SELECT list. Date and character literal values must be enclosed within single quotation marks. Each character string is output once for each row returned.

Using Literal Character Strings


SELECT last_name ||' is a '||job_id AS "Employee Details" FROM employees;

Duplicate Rows
The default display of queries is all rows, including duplicate rows.
SELECT department_id FROM employees;

Eliminating Duplicate Rows


Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.
SELECT DISTINCT department_id FROM employees;

Limiting Rows Using a Selection


EMPLOYEES

retrieve all employees in department 90

Limiting the Rows Selected


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

The WHERE clause follows the FROM clause.

Using the WHERE Clause


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

Comparison Conditions
Operator
= > >= < <= <>

Meaning
Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to

Using Comparison Conditions

SELECT last_name, salary FROM employees WHERE salary <= 3000;

Other Comparison Conditions


Operator Meaning

BETWEEN ...AND...
IN(set) LIKE IS NULL

Between two values (inclusive),

Match any of a list of values Match a character pattern Is a null value

Using the BETWEEN Condition


Use the BETWEEN condition to display rows based on a range of values.
SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500;

Lower limit

Upper limit

Using the IN Condition


Use the IN membership condition to test for values in a list.
SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN (100, 101, 201);

Using the LIKE Condition


Use the LIKE condition 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.
SELECT FROM WHERE first_name employees first_name LIKE 'S%';

Using the LIKE Condition


You can combine pattern-matching characters.
SELECT last_name FROM employees WHERE last_name LIKE '_o%';

You can use the ESCAPE identifier to search for the actual % and _ symbols.
SELECT employee_id, last_name, job_id FROM employees WHERE job_id LIKE '%SA\_%' ESCAPE '\';

Using the NULL Conditions


Test for nulls with the IS NULL operator.
SELECT last_name, manager_id FROM employees WHERE manager_id IS NULL;

Logical Conditions
Operator Meaning

AND
OR

Returns TRUE if both component conditions are true


Returns TRUE if either component condition is true Returns TRUE if the following condition is false

NOT

Using the AND Operator


AND requires both conditions to be true.
SELECT FROM WHERE AND employee_id, last_name, job_id, salary employees salary >=10000 job_id LIKE '%MAN%';

Using the OR Operator


SELECT FROM WHERE OR employee_id, last_name, job_id, salary employees salary >= 10000 job_id LIKE '%MAN%';

Using the NOT Operator


SELECT last_name, job_id FROM employees WHERE job_id NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP');

Rules of Precedence
Order Evaluated 1 2 3 4 5 6 7 8 Operator Arithmetic operators Concatenation operator Comparison conditions IS [NOT] NULL, LIKE, [NOT] IN [NOT] BETWEEN NOT logical condition AND logical condition OR logical condition

Override rules of precedence by using parentheses.

Rules of Precedence
SELECT FROM WHERE OR AND last_name, job_id, salary employees job_id = 'SA_REP' job_id = 'AD_PRES' salary > 15000;

ORDER BY Clause
Sort rows with the ORDER BY clause
ASC: ascending order, default DESC: descending order

The ORDER BY clause comes last in the SELECT statement.


SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date ;

Sorting by Multiple Columns


The order of ORDER BY list is the order of sort.
SELECT last_name, department_id, salary FROM employees ORDER BY department_id, salary DESC;

You can sort by a column that is not in the SELECT list.

Review
SQL History Basic SQL Select Statement Arithmetic Operators Where Clause Sorting

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy