Está en la página 1de 38

1

Writing Basic SQL Statements

1-1

Antrix Consultancy Services

Vasumathi M

Objectives
After After completing completing this this lesson, lesson, you you should should be be able able to to do do the the following: following: List List the the capabilities capabilities of of SQL SQL SELECT SELECT statements statements Execute Execute a a basic basic SELECT SELECT statement statement Differentiate Differentiate between between SQL SQL statements statements and and SQL*Plus SQL*Plus commands commands

1-2

Antrix Consultancy Services

Vasumathi M

Capabilities of SQL SELECT Statements


Selection Projection

Table 1

Join

Table 1

Table 1
1-3 Antrix Consultancy Services

Table 2
Vasumathi M

Basic SELECT Statement


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

SELECT SELECT identifies identifies what what columns columns FROM FROM identifies identifies which which table table

1-4

Antrix Consultancy Services

Vasumathi M

Writing SQL Statements


SQL SQL statements statements are are not not case case sensitive. sensitive. SQL SQL statements statements can can be be on on one one or or more more lines. lines. Keywords Keywords cannot cannot be be abbreviated abbreviated or or split split across across lines. lines. Clauses Clauses are are usually usually placed placed on on separate separate lines. lines. Tabs Tabs and and indents indents are are used used to to enhance enhance readability. readability.
1-5 Antrix Consultancy Services Vasumathi M

Selecting All Columns


SQL> SELECT * 2 FROM dept;

DEPTNO --------10 20 30 40

DNAME -------------ACCOUNTING RESEARCH SALES OPERATIONS

LOC ------------NEW YORK DALLAS CHICAGO BOSTON

1-6

Antrix Consultancy Services

Vasumathi M

Selecting Specific Columns


SQL> SELECT deptno, loc 2 FROM dept; DEPTNO --------10 20 30 40 LOC ------------NEW YORK DALLAS CHICAGO BOSTON

1-7

Antrix Consultancy Services

Vasumathi M

Column Heading Defaults


Default Default justification justification Left: Left: Date Date and and character character data data
Right: Right: Numeric Numeric data data

Default Default display: display: Uppercase Uppercase

1-8

Antrix Consultancy Services

Vasumathi M

Arithmetic Expressions
Create Create expressions expressions on on NUMBER NUMBER and and DATE DATE data data by by using using arithmetic arithmetic operators. operators.
Operator + * / Description Add Subtract Multiply Divide

1-9

Antrix Consultancy Services

Vasumathi M

Using Arithmetic Operators


SQL> SELECT ename, sal, sal+300 2 FROM emp; ENAME SAL SAL+300 ---------- --------- --------KING 5000 5300 BLAKE 2850 3150 CLARK 2450 2750 JONES 2975 3275 MARTIN 1250 1550 ALLEN 1600 1900 ... 14 rows selected.

1-10

Antrix Consultancy Services

Vasumathi M

Operator Precedence

/ +

Multiplication Multiplication and and division division take take priority priority over over addition addition and and subtraction. subtraction. Operators Operators of of the the same same priority priority are are evaluated evaluated from from left left to to right. right. Parentheses Parentheses are are used used to to force force prioritized prioritized evaluation evaluation and and to to clarify clarify statements. statements.
1-11 Antrix Consultancy Services Vasumathi M

Operator Precedence
SQL> SELECT ename, sal, 12*sal+100 2 FROM emp; ENAME SAL 12*SAL+100 ---------- --------- ---------KING 5000 60100 BLAKE 2850 34300 CLARK 2450 29500 JONES 2975 35800 MARTIN 1250 15100 ALLEN 1600 19300 ... 14 rows selected.

1-12

Antrix Consultancy Services

Vasumathi M

Using Parentheses
SQL> SELECT ename, sal, 12*(sal+100) 2 FROM emp; ENAME SAL 12*(SAL+100) ---------- --------- ----------KING 5000 61200 BLAKE 2850 35400 CLARK 2450 30600 JONES 2975 36900 MARTIN 1250 16200 ... 14 rows selected.

1-13

Antrix Consultancy Services

Vasumathi M

Defining a Null Value


A A null null is is a a value value that that is is unavailable, unavailable, unassigned, unassigned, unknown, unknown, or or inapplicable. inapplicable. A A null null is is not not the the same same as as zero zero or or a a blank blank space. space.
SQL> SELECT 2 FROM ename, job, comm emp;

ENAME JOB COMM ---------- --------- --------KING PRESIDENT BLAKE MANAGER ... TURNER SALESMAN 0 ... 14 rows selected.
1-14 Antrix Consultancy Services Vasumathi M

Null Values in Arithmetic Expressions


Arithmetic Arithmetic expressions expressions containing containing a a null null value value evaluate evaluate to to null. null.
SQL> select ename NAME, 12*sal+comm 2 from emp 3 WHERE ename='KING';

NAME 12*SAL+COMM ---------- ----------KING

1-15

Antrix Consultancy Services

Vasumathi M

Defining a Column Alias


Renames Renames a a column column heading heading Is Is useful useful with with calculations calculations Immediately Immediately follows follows column column name; name; optional optional AS AS keyword keyword between between column column name name and and alias alias Requires Requires double double quotation quotation marks marks if if it it contains contains spaces spaces or or special special characters characters or or is is case case sensitive sensitive
1-16 Antrix Consultancy Services Vasumathi M

Using Column Aliases


SQL> SELECT ename AS name, sal salary 2 FROM emp; NAME SALARY ------------- --------... SQL> SELECT ename "Name", 2 sal*12 "Annual Salary" 3 FROM emp; Name Annual Salary ------------- ------------...
1-17 Antrix Consultancy Services Vasumathi M

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

1-18

Antrix Consultancy Services

Vasumathi M

Using the Concatenation Operator


SQL> SELECT 2 FROM ename||job AS "Employees" emp;

Employees ------------------KINGPRESIDENT BLAKEMANAGER CLARKMANAGER JONESMANAGER MARTINSALESMAN ALLENSALESMAN ... 14 rows selected.
Antrix Consultancy Services

1-19

Vasumathi M

Literal Character Strings


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

1-20

Antrix Consultancy Services

Vasumathi M

Using Literal Character Strings


SQL> SELECT ename ||' '||'is a'||' '||job 2 AS "Employee Details" 3 FROM emp;

Employee Employee Details Details ------------------------------------------------KING KING is is a a PRESIDENT PRESIDENT BLAKE BLAKE is is a a MANAGER MANAGER CLARK CLARK is is a a MANAGER MANAGER JONES JONES is is a a MANAGER MANAGER MARTIN MARTIN is is a a SALESMAN SALESMAN ... ... 14 14 rows rows selected. selected.

1-21

Antrix Consultancy Services

Vasumathi M

Duplicate Rows
The The default default display display of of queries queries is is all all rows, rows, including including duplicate duplicate rows. rows.
SQL> SQL> 2 2 SELECT SELECT FROM FROM deptno deptno emp; emp;

DEPTNO --------10 30 10 20 ... 14 rows selected.

1-22

Antrix Consultancy Services

Vasumathi M

Eliminating Duplicate Rows


Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.
SQL> SELECT DISTINCT deptno 2 FROM emp;

DEPTNO --------10 20 30

1-23

Antrix Consultancy Services

Vasumathi M

SQL and SQL*Plus Interaction


SQL Statements

Buffer

SQL Statements

Server

SQL*Plus

SQL*Plus Commands Formatted Report

Query Results

1-24

Antrix Consultancy Services

Vasumathi M

SQL Statements Versus SQL*Plus Commands


SQL A language ANSI standard Keyword cannot be abbreviated Statements manipulate data and table definitions in the database
SQL statements SQL buffer
Antrix Consultancy Services

SQL*Plus An environment Oracle proprietary Keywords can be abbreviated Commands do not allow manipulation of values in the database

SQL*Plus commands

SQL*Plus buffer

1-25

Vasumathi M

Overview of SQL*Plus
Log Log in in to to SQL*Plus. SQL*Plus. Describe Describe the the table table structure. structure. Edit Edit your your SQL SQL statement. statement. Execute Execute SQL SQL from from SQL*Plus. SQL*Plus. Save Save SQL SQL statements statements to to files files and and append append SQL SQL statements statements to to files. files. Execute Execute saved saved files. files. Load Load commands commands from from file file to to buffer buffer to to edit. edit.
1-26 Antrix Consultancy Services Vasumathi M

Logging In to SQL*Plus
From Windows environment:

From command line: sqlplus [username[/password [@database]]]


1-27 Antrix Consultancy Services Vasumathi M

Displaying Table Structure


Use Use the the SQL*Plus SQL*Plus DESCRIBE DESCRIBE command command to to display display the the structure structure of of a a table. table.
DESC[RIBE] DESC[RIBE] tablename tablename

1-28

Antrix Consultancy Services

Vasumathi M

Displaying Table Structure


SQL> SQL> DESCRIBE DESCRIBE dept dept Name Name --------------------------------DEPTNO DEPTNO DNAME DNAME LOC LOC Null? Null? --------------NOT NOT NULL NULL Type Type ----------------------NUMBER(2) NUMBER(2) VARCHAR2(14) VARCHAR2(14) VARCHAR2(13) VARCHAR2(13)

1-29

Antrix Consultancy Services

Vasumathi M

SQL*Plus Editing Commands


A[PPEND] A[PPEND] text text C[HANGE] C[HANGE] // old old // new new C[HANGE] C[HANGE] // text text // CL[EAR] CL[EAR] BUFF[ER] BUFF[ER] DEL DEL DEL DEL n n DEL DEL m mn n
1-30 Antrix Consultancy Services Vasumathi M

SQL*Plus Editing Commands


I[NPUT] I[NPUT] I[NPUT] I[NPUT] text text L[IST] L[IST] L[IST] L[IST] n n L[IST] L[IST] m mn n R[UN] R[UN]
1-31

n n n n text text
Antrix Consultancy Services Vasumathi M

0 0 text text

SQL*Plus File Commands


SAVE SAVE filename filename GET GET filename filename START START filename filename @ @ filename filename EDIT EDIT filename filename SPOOL SPOOL filename filename

1-32

Antrix Consultancy Services

Vasumathi M

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

Use Use SQL*Plus SQL*Plus as as an an environment environment to: to: Execute Execute SQL SQL statements statements Edit Edit SQL SQL statements statements

1-33

Antrix Consultancy Services

Vasumathi M

Practice Overview
Selecting Selecting all all data data from from different different tables tables Describing Describing the the structure structure of of tables tables Performing Performing arithmetic arithmetic calculations calculations and and specifying specifying column column names names Using Using SQL*Plus SQL*Plus editor editor

1-34

Antrix Consultancy Services

Vasumathi M

1-35

Antrix Consultancy Services

Vasumathi M

1-36

Antrix Consultancy Services

Vasumathi M

1-37

Antrix Consultancy Services

Vasumathi M

exercise -1.pdf exercise-1.pdf

1-38

Antrix Consultancy Services

Vasumathi M

También podría gustarte