Virtual training week 4 structured query language (SQL)

Post on 14-Dec-2015

221 views 0 download

Transcript of Virtual training week 4 structured query language (SQL)

Virtual training week 4

structured query language (SQL)

Categories of SQL Statements1.1. Data manipulation language (DML) Data manipulation language (DML) statements

begin with INSERT, UPDATE, DELETE, or MERGE and are used to modify the table by entering new rows, changing existing rows, or removing existing rows.

2. Data definition language (DDL) statements set up, change, and remove data structures from the database. The keywords CREATE, ALTER, DROP, RENAME, and TRUNCATE begin DDL statements.

3. Transaction control (TCL) statements are used to manage the changes made by DML statements. Changes to the data are executed using COMMIT, ROLLBACK, and SAVEPOINT. TCL changes can be grouped together into logical transactions.

4. Data control language (DCL) keywords GRANT and REVOKE are used to give or remove access rights to the database and the structures within it.

a SELECT statement must include the following:

• A SELECTSELECT clause, which specifies the columns to be displayed

• A FROMFROM clause, which specifies the table containing the columns listed in the SELECT clause

Select ………...[column name]

From …….……..[table name]

A SELECT statement can do the following:

• ProjectionProjection: Used to choose columns in a table

• SelectionSelection: Used to choose rows in a table

• JoinJoin: Used to bring together data that is stored in different tables by creating a link between them (you will learn about this later!)

Aliases "friendly" nameColumn aliases let you rename columns in the

outputWhy column Alias- Renames a column heading- Is useful with calculations- Immediately follows the column name- May have the optional AS keyword between

the column name and alias - Requires double quotation marks if the alias

contains spaces or special characters or is case-sensitive

Concatenation (||)Concatenation (||)Concatenation means to connect or link together

in a series

Concatenation means to connect or link together in a series

Select columnname || columnname

From tablename

Select first_name || last_name

From empolyees

You cane use column alias to rename the connected columns.

DESCRIBEDESCRIBEdisplay the structure of a table.Why describe?DESCRIBE returns the table name, table

schema, tablespace name, indexes, triggers, constraints, and comments as well as the data types, primary and foreign keys, and which columns can be nullable.

DESC[RIBE] tablename;as

DESCRIBE departments;

DISTINCTDISTINCTUsed to eliminate duplicate rows.

DISTINCTDISTINCT automatically displays the output in alphabetical order.

Limiting rows selectedLimiting rows selected

Used to limit the number of rows retrieved from the quarry

notenote:  An alias cannot be used in the WHERE clause!

A WHERE clause contains a condition that must be met, and it directly follows the FROM clause in a SQL statement.

Where clauseWhere clause

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

FROM table[WHEREcondition(s)];

Select *

From employees

Where salary = 1500

Note/ conditions in where clause is case sensitive

comparison operators used with where clause

=   equal to

>   greater than

>= greater than or equal to

<   less than

<= less than or equal to

<> not equal to can be (!= or ^ =(

The default sort order is ascendingThe default sort order is ascending..

- Numeric values are displayed lowest to highest.

- Date values are displayed with the earliest value first.

- Character values are displayed in alphabetical order.

- Null values are displayed last in ascending order and first in descending order.

• It is also possible to order data by using a column aliasalias. The alias used in the SELECT statement is added to the ORDER BY clause.

SELECT title, year AS "Recording Date"

FROM d_cds

ORDER BY "Recording Date"

What does this order by mean?

Select first_name, last_name, salary, job_id

From employees

Where job_id = 10

Order by 3

GOOD LUCK

SEE YOU NEXT MEETING

Raafat Rashadraafat_rashad@yahoo.co.uk

raafat_rashad@hotmail.com