Top Banner
Muhammad Umair Basic SELECT Statement Oracle Database 11g Developer Track
28

Basic SQL Statments

Apr 14, 2017

Download

Software

Umair Shakir
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Basic SQL Statments

Muhammad Umair

Basic SELECT Statement

Oracle Database 11g Developer Track

Page 2: Basic SQL Statments

SQL Statements

SELECT Data retrieval

INSERTUPDATEDELETE

Data manipulation language (DML)

CREATEALTERDROPRENAMETRUNCATE

Data definition language (DDL)

COMMITROLLBACKSAVEPOINT

Transaction control

GRANTREVOKE

Data control language (DCL)

Page 3: Basic SQL Statments

SQL StatementsSELECT Retrieves data from the database

INSERTUPDATEDELETE

Enters new rows, changes existing rows, and removes unwanted rows from tables in the database, respectively. Collectively known as Data manipulation language(DML).

CREATEALTERDROPRENAMETRUNCATE

Set up, change, and remove data structures from tables. Collectively known as data definition language(DDL).

COMMITROLLBACKSAVEPOINT

Manage the changes made by DML statements. Changes to the data can be grouped together into logical transactions

GRANTREVOKE

Give or remove access rights to both the Oracle database and the structures within it. Collectively known as data control language(DCL).

Page 4: Basic SQL Statments

In its simplest form, a SELECT statement must include the following:•A SELECT clause , which specifies the columns to be displayed•A FROM clause, which specifies the table containing the columns listed in the SELECT clause

Basic SELECT Statement

SELECT is a list of one or more columns* selects all columnsDISTINCT suppresses duplicatescolumn|expression selects the named column or the

expressionalias gives selected columns different headings

FROM table specifies the table containing the columns

Page 5: Basic SQL Statments

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

Basic SELECT Statement

Page 6: Basic SQL Statments

• SQL statements are not case sensitive. • SQL statements can be on one or more lines.• Keywords cannot be abbreviated or split across lines.• Clauses are usually placed on separate lines.• Indents are used to enhance readability.

Writing SQL Statements

Page 7: Basic SQL Statments

SELECT *FROM departments;

Selecting All Columns of All RowsYou can display all columns of data in a table by following the SELECT keyword with an asterisk (*). In the example on the slide, the department table contains four columns:

DEPARTMENT_ID,DEPARTMENT_NAME,MANAGER_ID, and LOCATION_ID. The table contains seven rows, one for each department.

Selecting All Columns

Page 8: Basic SQL Statments

You can also display all columns in the table by listing all the columns after the SELECT keyword. For example, the following SQL statement, like the example on the slide, displays all columns and all rows of the DEPARTMENTS table:

SELECT department_id, department_name, manager_id, location_idFROM departments;

Selecting Specific Columns

Page 9: Basic SQL Statments

Create expressions with number and date data by using arithmetic operators.

Arithmetic Expressions

Operator Description+ Add- Subtract* Multiply/ Divide

Page 10: Basic SQL Statments

SELECT last_name, salary, salary + 300FROM employees;

Using Arithmetic Operators

Page 11: Basic SQL Statments

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

* / + -

Page 12: Basic SQL Statments

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

Using Parentheses

Page 13: Basic SQL Statments

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

SELECT last_name AS name, commission_pct commFROM employees;

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

Defining a Column Alias

Page 14: Basic SQL Statments

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

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

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

SELECT last_name ||‘ : 1 Month salary = '||salary MonthlyFROM employees;

Concatenation Operator

Page 15: Basic SQL Statments

Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.

SELECT DISTINCT department_idFROM employees

SELECT DISTINCT department_id, job_idFROM employees

Eliminating Duplicate Rows

Page 16: Basic SQL Statments

Use the iSQL*Plus DESCRIBE command to display the structure of a table.

DESCRIBE employees;

Displaying Table Structure

Page 17: Basic SQL Statments

SyntaxSELECT *|{[DISTINCT] column|expression alias],...} FROM table[WHERE condition(s)];

The WHERE clause can compare values in columns, literal values, arithmetic expressions, or functions. It consists of three elements:•Column name•Comparison condition•Column name, constant, or list of values

Limiting the Rows Selected

Page 18: Basic SQL Statments

SELECT employee_id, last_name, job_id, department_idFROM employeesWHERE department_id = 90;

Using the WHERE Clause

Page 19: Basic SQL Statments

• 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 DD-MON-RR.

SELECT last_name, job_id, department_idFROM employeesWHERE last_name ='Goyal';

Character Strings and Dates

Page 20: Basic SQL Statments

Comparison Conditions

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

Between two values (inclusive)

IN(set) Match any of a list of valuesLIKE Match a character patternIS NULL Is a null value

Page 21: Basic SQL Statments

Using Comparison Conditions

SELECT last_name, salaryFROM employeesWHERE salary <= 3000;

SELECT last_name, salaryFROM employeesWHERE salary BETWEEN 2500 AND 3500;

SELECT employee_id, last_name, salary, manager_idFROM employeesWHERE manager_id IN (100, 101, 201);

Page 22: Basic SQL Statments

Using Comparison ConditionsSELECT employee_id, manager_id, department_id

FROM employeesWHERE last_name IN ('Hartstein’, 'Vargas');

% Represents any sequence of zero or more characters_ Represents any single character

SELECT first_nameFROM employeesWHERE first_name LIKE ‘_S%';

SELECT first_nameFROM employeesWHERE first_name LIKE ‘%AR_';

Page 23: Basic SQL Statments

Logical Conditions

Operator MeaningAND Returns TRUE if both component

conditions are trueOR Returns TRUE if either

component conditions are true

NOT Returns TRUE if the following condition is false

Page 24: Basic SQL Statments

Using the AND Operator

SELECT employee_id, last_name, job_id, salaryFROM employeesWHERE salary >=10000AND job_id LIKE '%MAN%‘;

Page 25: Basic SQL Statments

Using the OR Operator

SELECT employee_id, last_name, job_id, salaryFROM employeesWHERE salary >=10000OR job_id LIKE '%MAN%‘;

Page 26: Basic SQL Statments

Using the NOT Operator

SELECT employee_id, last_name, job_id, salaryFROM employeesWHERE job_id NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP');

Page 27: Basic SQL Statments

ORDER BY Clause

•Sort rows with the ORDER BY clause– ASC : ascending order (the default order)– DESC : descending order•The ORDER BY clause comes last in the SELECT statement.

SELECT last_name, job_id, department_id, hire_dateFROM employeesORDER BY hire_date;

SELECT last_name, job_id, department_id, hire_dateFROM employeesORDER BY hire_date DESC;

Page 28: Basic SQL Statments

Sorting by Column Alias

SELECT employee_id, last_name, salary*12 annsalFROM employees ORDER BY annsal;

Sorting by Multiple Columns

SELECT last_name, department_id, salaryFROM employeesORDER BY department_id, salary DESC;