Top Banner
1 Databases
22

1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

Dec 21, 2015

Download

Documents

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: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

1

Databases

Page 2: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

2

Simple selects

• The full syntax of the SELECT statement is complex, but the main clauses can be summarized as:

• SELECT select_list[INTO new_table_name]FROM table_list[WHERE search_conditions][GROUP BY group_by_list][HAVING search_conditions][ORDER BY order_list [ASC | DESC] ]

• In this lesson, we will address only those clauses in black.

Page 3: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

3

Projection

• Projection picks only rows that satisfy a condition:

• Pick only books with Harry Potter in the title.

• Note that each row wraps around, making it difficult to read.

• Find the tables SQL> select * from cat where table_name not like 'BIN$%‘;

Page 4: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

4

Date functions• Commonly used date functions are:

– sysdate – next_day– add_months– last_day– months_between– least– greatest– round– trunc

Page 5: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

5

Date functions and the DUAL table.

• Current date and time:SQL> select sysdate from dual;

SYSDATE

---------

30-SEP-05

• Dual:– This supplies values for system variables.

Current date and time is one of them. We will come across more later.

Page 6: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

6

Sample date functionsSQL> SELECT 2 datepublished AS "Date", 3 TO_CHAR(datepublished,'DAY'), 4 NEXT_DAY(datepublished,'MONDAY') AS "Monday

following", 5 LAST_DAY(datepublished) AS "Last day of month", 6 ADD_MONTHS (datepublished,3) AS "3 months later" 7 FROM BOOK;

Page 7: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

7

Meaning…• TO_CHAR(date,format)

– Converts the date to the specified format.

• NEXT_DAY(date,dayofweek)– Gives the date of the next ‘dayofweek’ after the date given.

• LAST_DAY(date)– Gives the last day of the month in the date specified.

• ADD_MONTHS (date,int)– Adds int months to the given date.

Page 8: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

8

Date functions

• SYSDATE gives current date• NEXT_DAY(d,day) where d is a date and day is a string

representing a day of the week. – E.g. next_day(’14-dec-2005’,’Monday’) will return ’19-dec-2005’

• ADD_MONTHS(d,count) adds n months to d.• LAST_DAY(d) returns the date corresponding to the last

day of the month in which d belongs.• MONTHS_BETWEEN(d1,d2)• LEAST(d1,d2,…,dn)• GREATEST(d1,…,dn)• TRUNC(d) returns the date (d) with the time at midnight.

Page 9: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

9

Functions in SQL

• There are many types of functions provided. • The ones that are used most are:

– Date and Time functions– Mathematical functions– String functions

• There follows a list of all functions in these categories.

• We will practice only the most popularly used.

Page 10: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

10

All about dates

• Dates are relative – i.e. the date and time are the same function.

• The current date and time depends on where you are in the world.

• The date format '12-dec-2005' will work, but NOT ’12-dec-2005’. – The apostrophes, or quotes, are different.

• Microsoft Word / PowerPoint will automatically change from ' to ‘.

Page 11: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

11

Formatting the date• TO_CHAR(d,format) returns the date in the format specified:

– Mm returns month number– Mon returns the month in 3-character format– D returns the day number in the week– DD returns the day number in the month– DDD returns the day number in the year– DY gives the weekday in 3-character format– DAY gives the weekday name– Y returns the last digit of the year– Yy returns the last 2 digits of the year– Yyyy returns the 4-digit year– Hh12 returns the hours of the day(1 -12)– Hh24 returns the hours of the day (1 – 24)– Mi returns the minutes of the hour– Ss returns the seconds of the minute– AM returns AM or PM

Page 12: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

12

resources

• These sites are helpful:– http://www.techonthenet.com/oracle/index.php– http://www.ss64.com/orasyntax/

Page 13: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

13

To put a name on a column• Use the ‘AS’ clause to give a name to a column.

– Unitprice AS Price or– UnitPrice AS “Unit Price”– Note double quotes.

• This can be used on any column, but is especially useful in a derived column.

• New columns can be derived from existing fields:• E.g. the value of an item in stock is the number in stock by the unit

price. • If the alias contains a space, surround it with double quotes:SQL>

SELECT datepublished AS “Date Published" FROM book;

Page 14: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

14

String manipulation

• Concatenation: use || instead of +

• Pad out a string (from the left) to a specified length:– Lpad(string,length,’padding char’)– Rpad does the same, but pads from the right.

• Trim strings of characters uses– Ltrim(string,’trim char’)– Rtrim trims from the right.

Page 15: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

15

Look up…

• Lower(string)

• Upper(string)

• Length(string)

• Substr(string,start,[n])

• Instr(string,’chars’[,start [,n]])

Page 16: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

16

Exercises• Write a query to return today’s date.• Write a query to return the title of each book and the number of months since it was published,

giving the following output:The Legend of Spud Murphy 69.6937855The Legend of Captain Crow's teeth 33.6937855Don't Open that Box 93.6937855Benny and Babe 129.693785Artemis Fowl 81.6937855Harry Potter and the Goblet of Fire 98.5324951Harry Potter and the Philosopher's Stone 86.5324951Harry Potter and the Chamber of Secrets 74.5324951Harry Potterand the Prisoner of Azkhaban 62.5324951Harry Potter + the Order of the Phoenix 38.5324951Harry Potter and the Half-Blood Prince 14.5324951Cirque Du Freak 44.5324951Tunnel of Blood 23.5324951Up, up and Away 189.693785Tom and Pippo 249.693785

Page 17: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

17

Refine:

• Refine your query, to round the months:Harry Potter and the Half-Blood Prince 15

Cirque Du Freak 45

Tunnel of Blood 24

Up, up and Away 190

Tom and Pippo 250

15 rows selected.

Page 18: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

18

Refine it again, to return years, instead of months

Harry Potter and the Half-Blood Prince 1

Cirque Du Freak 4

Tunnel of Blood 2

Up, up and Away 16

Tom and Pippo 21

15 rows selected.

Page 19: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

19

Refine the output

Concatenate (string together) the results (replace the , with ||):

The Legend of Spud Murphy6The Legend of Captain Crow's teeth3Don't Open that Box8Benny and Babe11Artemis Fowl7Harry Potter and the Goblet of Fire8Harry Potter and the Philosopher's Stone7Harry Potter and the Chamber of Secrets6Harry Potterand the Prisoner of Azkhaban5Harry Potter + the Order of the Phoenix3Harry Potter and the Half-Blood Prince1Cirque Du Freak4Tunnel of Blood2Up, up and Away16Tom and Pippo21

15 rows selected.

Page 20: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

20

Embed text strings in the output

• E.g. Select ‘Title ‘||title…Title The Legend of Spud Murphy6Title The Legend of Captain Crow's teeth3Title Don't Open that Box8Title Benny and Babe11Title Artemis Fowl7Title Harry Potter and the Goblet of Fire8Title Harry Potter and the Philosopher's Stone7Title Harry Potter and the Chamber of Secrets6Title Harry Potterand the Prisoner of Azkhaban5Title Harry Potter + the Order of the Phoenix3Title Harry Potter and the Half-Blood Prince1Title Cirque Du Freak4Title Tunnel of Blood2Title Up, up and Away16Title Tom and Pippo21

15 rows selected.

Page 21: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

21

Now add more text…------------------------------------------------------------------------The title 'The Legend of Spud Murphy' is 6 years oldThe title 'The Legend of Captain Crow's teeth' is 3 years oldThe title 'Don't Open that Box' is 8 years oldThe title 'Benny and Babe' is 11 years oldThe title 'Artemis Fowl' is 7 years oldThe title 'Harry Potter and the Goblet of Fire' is 8 years oldThe title 'Harry Potter and the Philosopher's Stone' is 7 years oldThe title 'Harry Potter and the Chamber of Secrets' is 6 years oldThe title 'Harry Potterand the Prisoner of Azkhaban' is 5 years oldThe title 'Harry Potter + the Order of the Phoenix' is 3 years oldThe title 'Harry Potter and the Half-Blood Prince' is 1 years oldThe title 'Cirque Du Freak' is 4 years oldThe title 'Tunnel of Blood' is 2 years oldThe title 'Up, up and Away' is 16 years oldThe title 'Tom and Pippo' is 21 years old

15 rows selected.

SQL>

Page 22: 1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.

22

And add a heading name

Titles and their ages---------------------------------------------------------------------------The title 'The Legend of Spud Murphy' is 6 years oldThe title 'The Legend of Captain Crow's teeth' is 3 years oldThe title 'Don't Open that Box' is 8 years oldThe title 'Benny and Babe' is 11 years oldThe title 'Artemis Fowl' is 7 years oldThe title 'Harry Potter and the Goblet of Fire' is 8 years oldThe title 'Harry Potter and the Philosopher's Stone' is 7 years oldThe title 'Harry Potter and the Chamber of Secrets' is 6 years oldThe title 'Harry Potterand the Prisoner of Azkhaban' is 5 years oldThe title 'Harry Potter + the Order of the Phoenix' is 3 years oldThe title 'Harry Potter and the Half-Blood Prince' is 1 years oldThe title 'Cirque Du Freak' is 4 years oldThe title 'Tunnel of Blood' is 2 years oldThe title 'Up, up and Away' is 16 years oldThe title 'Tom and Pippo' is 21 years old

15 rows selected.

Remember, to put in a heading with spaces, the heading needs to be surrounded by double quotes.