Virtual training week 4 structured query language (SQL)

47
Virtual training week 4 structured query language (SQL)

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

Page 1: Virtual training week 4 structured query language (SQL)

Virtual training week 4

structured query language (SQL)

Page 2: Virtual training week 4 structured query language (SQL)
Page 3: Virtual training week 4 structured query language (SQL)
Page 4: Virtual training week 4 structured query language (SQL)
Page 5: Virtual training week 4 structured query language (SQL)
Page 6: Virtual training week 4 structured query language (SQL)
Page 7: Virtual training week 4 structured query language (SQL)
Page 8: Virtual training week 4 structured query language (SQL)
Page 9: Virtual training week 4 structured query language (SQL)
Page 10: Virtual training week 4 structured query language (SQL)
Page 11: Virtual training week 4 structured query language (SQL)
Page 12: Virtual training week 4 structured query language (SQL)
Page 13: Virtual training week 4 structured query language (SQL)
Page 14: Virtual training week 4 structured query language (SQL)
Page 15: Virtual training week 4 structured query language (SQL)
Page 16: Virtual training week 4 structured query language (SQL)
Page 17: Virtual training week 4 structured query language (SQL)
Page 18: Virtual training week 4 structured query language (SQL)
Page 19: 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.

Page 20: Virtual training week 4 structured query language (SQL)
Page 21: Virtual training week 4 structured query language (SQL)

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]

Page 22: Virtual training week 4 structured query language (SQL)

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!)

Page 23: Virtual training week 4 structured query language (SQL)
Page 24: Virtual training week 4 structured query language (SQL)
Page 25: Virtual training week 4 structured query language (SQL)
Page 26: Virtual training week 4 structured query language (SQL)
Page 27: Virtual training week 4 structured query language (SQL)
Page 28: Virtual training week 4 structured query language (SQL)

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

Page 29: Virtual training week 4 structured query language (SQL)
Page 30: Virtual training week 4 structured query language (SQL)

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.

Page 31: Virtual training week 4 structured query language (SQL)
Page 32: Virtual training week 4 structured query language (SQL)

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;

Page 33: Virtual training week 4 structured query language (SQL)
Page 34: Virtual training week 4 structured query language (SQL)

DISTINCTDISTINCTUsed to eliminate duplicate rows.

DISTINCTDISTINCT automatically displays the output in alphabetical order.

Page 35: Virtual training week 4 structured query language (SQL)

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

Page 36: Virtual training week 4 structured query language (SQL)
Page 37: Virtual training week 4 structured query language (SQL)

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

FROM table[WHEREcondition(s)];

Select *

From employees

Where salary = 1500

Note/ conditions in where clause is case sensitive

Page 38: Virtual training week 4 structured query language (SQL)
Page 39: Virtual training week 4 structured query language (SQL)

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 ^ =(

Page 40: Virtual training week 4 structured query language (SQL)
Page 41: Virtual training week 4 structured query language (SQL)
Page 42: Virtual training week 4 structured query language (SQL)
Page 43: Virtual training week 4 structured query language (SQL)
Page 44: Virtual training week 4 structured query language (SQL)

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.

Page 45: Virtual training week 4 structured query language (SQL)

• 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"

Page 46: Virtual training week 4 structured query language (SQL)

What does this order by mean?

Select first_name, last_name, salary, job_id

From employees

Where job_id = 10

Order by 3

Page 47: Virtual training week 4 structured query language (SQL)

GOOD LUCK

SEE YOU NEXT MEETING

Raafat [email protected]

[email protected]