Lecture 1 at A Glance: Lecture No 2: Writing Basic SQL SELECT Statements

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

National Institute of Electronics

Lecture 1 At a Glance
In the last lecture we have completed following topics:

• Describe Data, Data types and Database.


• Describe and explain functions/processing on data.
• Describe Database Types and Types of Centralized
Database.
• Describe use of Database, File based system.
• Advantages of Database.
• Describe System Development Life Cycle.
• Define Relational Database Concepts
• Entity Relationship Model
• Relating Multiple Tables
• Relational Database Terminology
• Describe Structured Query Language and its types.

1 Lecture No 2: Writing Basic SQL SELECT Statements


Lecture 2
Writing Basic SQL
SELECT Statements
National Institute of Electronics

Objectives
After completing this lecture, you should be able to
do the following:

• List the capabilities of SQL SELECT statements

• Describe and use different tools to communicate with


Oracle Database server using Structured Query
Language (SQL)

• Execute a basic SELECT statement

• Differentiate between SQL statements and iSQL*Plus


commands
3 Lecture No 2: Writing Basic SQL SELECT Statements
National Institute of Electronics

Capabilities of SQL SELECT Statements


A SELECT statement retrieves information from the database. Using SELECT
statement you can do the following:
• Projection: To choose the columns in a table that
you want to returned by the query. You can choose
as few or as many columns of the table as you
require.
• Selection: To choose the rows in a table. You can
use various criteria to restrict the required rows.

• Joining: To bring together data that is stored in


different tables by creating a link between them.

Join

Table 1 Table 2
4 Lecture No 2: Writing Basic SQL SELECT Statements
National Institute of Electronics

Tools for Executing SQL Statements

SQL*Plus iSQL*Plus

•Runs from Oracle Server and from •Runs using internet Explorer
any computer where Oracle Database from any computer on the network
client is installed
•No need to install Oracle client

5 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

hr

**
ora

6 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

7 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Tools for Executing SQL Statements

SQL*Plus iSQL*Plus

Runs from Oracle Server and from Runs using internet Explorer
any computer where Oracle Database from any computer on the network
client is installed
No need to install Oracle client

8 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

hr
**

9 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

10 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Basic SELECT Statement


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

A SELECT Statement must include the following:


• A SELECT clause which identifies what columns to be
displayed
• A FROM clause which identifies which table
In the syntax:
SELECT is a list of one or more columns
* Selects all columns
DISTINCT suppresses duplicates
Column | expression selects the named column or the expression
alias gives selected columns different headings
FROM table specifies the table containing the columns

11 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Explore Sample Human Resource Schema


Schema: A group of related database objects assigned to a database user.
In Oracle 10g SQL Course we will use following tables in examples:

Regions : Contains regions information like Asia, Europe etc


Countries : Names of Countries in specific regions like Pakistan, Japan etc
Locations : Information about Locations in specific countries
Departments : Information about departments located at any specific location
Jobs : This table contains jobs name with salary ranges
Employees : Information about employees contains hiring date, salary,
department information etc
Job_History : Contains employees job history information for any particular
previous job title

Explore HR Schema

12 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Selecting All Columns


SELECT *
FROM departments;

… …

13 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Selecting Specific Columns


SELECT department_id, location_id
FROM departments;

……

14 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Writing SQL Statements


• SQL statements are not case sensitive.
For Example: OR

• SQL statements can be on one or more lines.


For Example: OR

• Keywords cannot be abbreviated or split across lines.


For Example: OR

• Clauses are usually placed on separate lines.


For Example:

• Indents are used to enhance readability.


For Example:
15 Lecture No 2: Writing Basic SQL SELECT Statements
National Institute of Electronics

Column Heading Defaults


• iSQL*Plus:
– Default heading justification: Center
– Default heading display: Uppercase

• SQL*Plus:
– Character and Date column headings are left- justified
– Number column headings are right-justified
– Default heading display: Uppercase

16 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Arithmetic Expressions

Create expressions with number and date data by using


arithmetic operators.

Operator Description

+ Add

- Subtract

* Multiply

/ Divide

17 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Using Arithmetic Operators

+ - * /
SELECT
SELECT first_name,
first_name, last_name,
last_name, salary,
salary, salary
salary ++ 300
300
FROM
FROM employees;
employees;

SELECT
SELECT first_name,
first_name, last_name,
last_name, salary,
salary, salary
salary -- 300
300
FROM
FROM employees;
employees;

SELECT
SELECT first_name,
first_name, last_name,
last_name, salary,
salary, salary
salary ** 22
FROM
FROM employees;
employees;

SELECT
SELECT first_name,
first_name, last_name,
last_name, salary,
salary, salary
salary // 1000
1000
FROM
FROM employees;
employees;

18 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

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.
3*2+10=16,
3*2+10=16,3*(2+10)=36
3*(2+10)=36

19 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

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


Using Parentheses
Parentheses are used to force prioritized evaluation and to clarify statements.

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


20 Lecture No 2: Writing Basic SQL SELECT Statements
National Institute of Electronics

Defining a Null Value


• A null is a value that is unavailable, unassigned or
unknown.
• A null is not the same as zero or a blank space.
SELECT last_name, job_id, salary, commission_pct
FROM employees;

21 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Null Values
in Arithmetic Expressions

Arithmetic expressions containing a null value


evaluate to null.
SELECT last_name, 12*salary*commission_pct
FROM employees;

22 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

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
23 Lecture No 2: Writing Basic SQL SELECT Statements
National Institute of Electronics

Using Column Aliases

SELECT last_name AS name, commission_pct comm


FROM employees;

SELECT last_name "Name", salary*12 "Annual Salary"


FROM employees;

24 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

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

25 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Using the Concatenation Operator

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

26 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

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.

27 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Using Literal Character Strings


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

28 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Duplicate Rows

The default display of queries is all rows, including


duplicate rows.
SELECT
SELECT department_id
department_id
FROM
FROM employees;
employees;

29 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Eliminating Duplicate Rows

Eliminate duplicate rows by using the DISTINCT


keyword in the SELECT clause.
SELECT DISTINCT department_id
FROM employees;

30 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

SQL and iSQL*Plus Interaction

SQL statements
iSQL*Plus Oracle
Internet server
Browser

iSQL*Plus Query results


commands

Formatted report

Client

31 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

SQL Statements Versus


iSQL*Plus Commands
SQL iSQL*Plus
• A language • An environment
• ANSI standard • Oracle proprietary
• Keyword cannot be • Keywords can be
abbreviated abbreviated
• Statements manipulate • Commands do not allow
data and table definitions manipulation of values in
in the database the database
• Runs on a browser
• Centrally loaded, does not
have to be implemented
on each machine
SQL iSQL*Plus
statements commands

32 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Overview
of iSQL*Plus

After you log into iSQL*Plus, you can:


• Describe the table structure
• Edit your SQL statement
• Execute SQL from iSQL*Plus
• Save SQL statements to files and append SQL
statements to files
• Execute statements stored in saved files
• Load commands from a text file into the iSQL*Plus Edit
window

33 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Logging In to iSQL*Plus

From your Windows browser environment:

34 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

The iSQL*Plus Environment


10 8 9

1 7

2 3 4 5

35 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Displaying Table Structure

Use the iSQL*Plus DESCRIBE command to display


the structure of a table.

DESC[RIBE]
DESC[RIBE] tablename
tablename

36 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Displaying Table Structure

DESCRIBE
DESCRIBE employees
employees

37 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Interacting with
Script Files

SELECT last_name, hire_date, salary


FROM employees; 1

38 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Interacting with
Script Files
1

D:\temp\emp_sql.htm

SELECT last_name, hire_date, salary 2


FROM employees;

39 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Interacting with
Script Files

DESCRIBE employees
SELECT employee_id, first_name, last_name, job_id 1
FROM employees;

3 2

40 Lecture No 2: Writing Basic SQL SELECT Statements


National Institute of Electronics

Summary
In this lecture, we have covered following topics:

• List the capabilities of SQL SELECT statements

• Describe and use different tools to communicate with


Oracle Database server using Structured Query
Language (SQL)

• Execute a basic SELECT statement

• Differentiate between SQL statements and iSQL*Plus


commands

41 Lecture No 2: Writing Basic SQL SELECT Statements

You might also like