Top Banner
5/14/2015 Charity School Events Bachelor of Science in Business Computing Year 3 Database Systems- Oracle SQL Project Agnieszka Roguska A00188976
83
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: Agnieszka_Roguska_CharitySchoolEvents

5/14/2015

Charity School Events

Bachelor of Science in Business

Computing Year 3

Database Systems- Oracle SQL Project

Agnieszka Roguska A00188976

Page 2: Agnieszka_Roguska_CharitySchoolEvents

1

Agnieszka Roguska A00188976

Table of Content:

1. Introduction 3

2. Original Data 4

3. ERD Entity Relationship Diagram 8

4. Create Table Statement 9

5. Insert Statement 11

6. Tables 18

7. Single Table Queries 20

8. Joins 27

9. Summary Queries 36

10. Subqueries 43

11. Update, Inserts, Deletes 53

12. Views 68

Page 3: Agnieszka_Roguska_CharitySchoolEvents

2

Agnieszka Roguska A00188976

1. Introduction

The aim of this project was to examine all the data in relation to

Charity School Events and then prepare it, in order to build a

database using Oracle SQL. The table names and column names

were supplied and therefore the initial step was to find the

relationships between each table nd then populate the tables,

these relationships were modelled on an Entity Relationship

Diagram (Erd) and outline the Primary Key and Foreign Key in

each table.

I will cover every aspect of the SQL Command Set: Data

Manipulation Language, Data Description Language, Data

Controlling Language And Select Statement.

Page 4: Agnieszka_Roguska_CharitySchoolEvents

3

Agnieszka Roguska A00188976

2. Original Data

Charity School Events-CSE

Patron (Patron_Id, Name (eg Mercy Sisters, Christian Brothers, Offaly VEC, Dublin VEC, etc), Address, Town, County, Phone_No, Email_Add, Web_Site) School (School_ID, Name, Address, Town, County, Num_Students, Email_Add, Web_Site, Phone_No, Category (Secondary, Community, Vocational), Gender (Mixed, Boys, Girls), Patron_Id) Charity (Charity_Id, Name, Address, City, County, Email, Web_Site,

Annual_Budget, Yr_Founded, Mission (Health, Education, Famine Relief, Poverty

etc))

Event (Event_Id, Description, Event_Date, Target_Amount (of money to be raised),

Actual_Amount (of money raised), Start_Time, End_Time, Total_Raised, Charity_Id,

School_Id, Spot_Prize_Value(ie the value of all spot prizes donated by the disco

Sponsor), Sponsor_ID, Host_ID)

Host (Host_ID, FName, Sname, Address, Town, County, Date_Of_Birth,

Telephone_No, Mobile_No, Specialism (Radio, TV, Sport, Politics)

Sponsor (Sponsor_Id, Name, Address, Town, County, Year_Established,

Email_Address, Web_Site, Business_Type (Manufacturing, Finance, Grocery, Public

House etc), Num_Employees, Annual_Turnover)

Page 5: Agnieszka_Roguska_CharitySchoolEvents

4

Agnieszka Roguska A00188976

Charity School Events-CSE with PK and FK

Charity (Charity_Id, Name, Address, City, County, Email, Web_Site, Annual_Budget,

Yr_Founded, Mission (Health, Education, Famine Relief, Poverty etc))

Host (Host_ID, FName, Sname, Address, Town, County, Date_Of_Birth, Telephone_No,

Mobile_No, Specialism (Radio, TV, Sport, Politics)

Sponsor (Sponsor_Id, Name, Address, Town, County, Year_Established, Email_Address,

Web_Site, Business_Type (Manufacturing, Finance, Grocery, Public House etc),

Num_Employees, Annual_Turnover)

Patron (Patron_Id, Name (eg Mercy Sisters, Christian Brothers, Offaly VEC, Dublin VEC, etc), Address, Town, County, Phone_No, Email_Add, Web_Site)

School (School_ID, Name, Address, Town, County, Num_Students, Email_Add, Web_Site, Phone_No, Category (Secondary, Community, Vocational), Gender (Mixed, Boys, Girls), Patron_Id)

Event (Event_Id, Description, Event_Date, Target_Amount (of money to be raised),

Actual_Amount (of money raised), Start_Time, End_Time, Total_Raised, Charity_Id,

School_ID, Spot_Prize_Value(ie the value of all spot prizes donated by the disco Sponsor),

Sponsor_ID, Host_ID)

Charity

Charity_Id

Name

Address

City

County

Page 6: Agnieszka_Roguska_CharitySchoolEvents

5

Agnieszka Roguska A00188976

Email

Web_Site

Annual_Budget

Yr_Founded

Mission

Host

Host_ID

FName

Sname

Address

Town

County,

Date_Of_Birth

Telephone_No

Mobile_No

Specialism

Sponsor

Sponsor_Id

Name

Address

Town

County

Year_Established

Email_Address

Page 7: Agnieszka_Roguska_CharitySchoolEvents

6

Agnieszka Roguska A00188976

Web_Site

Business_Type

Num_Employees

Annual_Turnover)

Patron Patron_Id Name Address Town County Phone_No Email_Add Web_Site School School_ID Name Address Town County Num_Students Email_Add Web_Site Phone_No Category Gender Patron_Id

Event

Event_Id Description Event_Date Target_Amount Actual_Amount Start_Time End_Time

Page 8: Agnieszka_Roguska_CharitySchoolEvents

7

Agnieszka Roguska A00188976

Total_Raised Charity_Id School_ID Spot_Prize_Value Sponsor_ID Host_ID

3. ERD Entity Relationship Diagram

EXTENDED ERD with table column names

Page 9: Agnieszka_Roguska_CharitySchoolEvents

8

Agnieszka Roguska A00188976

4. Create Table Statement

drop table Host ;

create table Host (

Host_ID Number ( 3 ) ,

Fname Varchar2 ( 7 ) ,

Sname Varchar2 ( 9 ) ,

Address Varchar2 ( 32 ) ,

Town Varchar2 ( 17 ) ,

County Varchar2 ( 12 ) ,

Date_Of_Birth Date ,

Telephone_No Varchar2 ( 11 ) ,

Mobile_No Varchar2 ( 12 ) ,

Specialism Varchar2 ( 8 ) );

drop table Sponsor ;

create table Sponsor (

Sponsor_Id Number ( 2 ) ,

Name Varchar2 ( 35 ) ,

Address Varchar2 ( 32 ) ,

Town Varchar2 ( 14 ) ,

County Varchar2 ( 9 ) ,

Year_Established Number ( 4 ) ,

Email_Address Varchar2 ( 43 ) ,

Web_Site Varchar2 ( 43 ) ,

Business_Type Varchar2 ( 12 ) ,

Num_Employees Number ( 4 ) ,

Annual_Turnover Number ( 5 ) );

Page 10: Agnieszka_Roguska_CharitySchoolEvents

9

Agnieszka Roguska A00188976

drop table Patron ;

create table Patron (

Patron_Id Number ( 3 ) ,

Name Varchar2 ( 29 ) ,

Address Varchar2 ( 26 ) ,

Town Varchar2 ( 14 ) ,

County Varchar2 ( 23 ) ,

Phone_No Varchar2 ( 11 ) ,

Email_Add Varchar2 ( 37 ) ,

Web_Site Varchar2 ( 36 ) );

drop table School ;

create table School (

School_ID Number ( 3 ) ,

Name Varchar2 ( 12 ) ,

Address Varchar2 ( 26 ) ,

Town Varchar2 ( 12 ) ,

County Varchar2 ( 10 ) ,

Num_Students Number ( 8 ) ,

Email_Add Varchar2 ( 22 ) ,

Web_Site Varchar2 ( 19 ) ,

Phone_No Varchar2 ( 9 ) ,

Category Varchar2 ( 10 ) ,

Gender Varchar2 ( 5 ) ,

Patron_Id Number ( 3 ) );

drop table Event ;

create table Event (

Event_Id Number ( 3 ) ,

Description Varchar2 ( 18 ) ,

Event_Date Date ,

Target_Amount Number ( 5 ) ,

Actual_Amount Number ( 5 ) ,

Start_Time Number ( 9 ) ,

End_Time Number ( 17 ) ,

Total_Raised Number ( 5 ) ,

Charity_Id Number ( 2 ) ,

School_Id Number ( 3 ) ,

Spot_Prize_Value Number ( 5 ) ,

Sponsor_ID Number ( 2 ) ,

Host_ID Number ( 3 ) );

Page 11: Agnieszka_Roguska_CharitySchoolEvents

10

Agnieszka Roguska A00188976

5. Insert Statement

/* Table Name:Charity */ /*Charity_Id,Name,Address,City,County,Email,Web_Site,Annual_Budget,Yr_Founded,Mission */ Insert into Charity values (1,'William Cavendish','18 Spar Centre','Banagher','Offaly','info@William Cavendish.ie','www.WilliamCavenish.ie',12000,1986,'Health'); Insert into Charity values (2,'Thomas Pelham','73 Main Street','Crookstown','Cork','info@Thomas Pelham.ie','www.ThomasPelham.ie',20000,1981,'Education'); Insert into Charity values (3,'John Stuart','Famhealth Medipark Centre','Ballyroe','Kildare','info@John Stuart.ie',null,28000,1976,'Famine Relief'); Insert into Charity values (4,'George Grenville','38 Church Street','Arklow','Wicklow','info@George Grenville.ie','www.GeorgeGrenville.ie',36000,1971,'Poverty'); Insert into Charity values (5,'Charles Watson Wentworth','51 A Somerset Street','Fenit','Kerry','info@Charles Watson Wentworth.ie','www.CharlesWatsonWentworth.ie',44000,1966,'Health'); Insert into Charity values (6,'William Pitt ','18 Bathurst Street','Ballysax','Kildare','info@William Pitt .ie','www.WilliamPitt.ie',52000,1961,'Education'); Insert into Charity values (7,'Augustus Henry Fitzroy','4 B Allen Street','Gortnahoo','Tipperary','info@Augustus Henry Fitzroy.ie','www.AugustusHenryFitzroy.ie',60000,1990,'Famine Relief'); Insert into Charity values (8,'Frederick North','The Colcade','Banagher','Offaly','info@Frederick North.ie','www.FrederickNorth.ie',68000,1987,'Poverty'); Insert into Charity values (9,'Charles Watson Wentworth','7 Greenacres ','Crookstown','Cork','info@Charles Watson Wentworth.ie','www.CharlesWatsonWentworth.ie',76000,1984,'Health'); Insert into Charity values (10,'William Petty','20 Main Street','Ballyroe','Kildare','info@William Petty.ie','www.WilliamPetty.ie',20000,1981,'Education'); Insert into Charity values (11,'William Henry Cavenish Bentinck','Neptunes Terrace','Bellanode','Monaghan','help@William Henry Cavenish Bentinck.ie',null,15000,1978,'Famine Relief'); Insert into Charity values (12,'William Pitt','Schelde Street','Clifden','Galway','help@William Pitt.ie','www.WilliamPitt.ie',10000,1999,'Poverty'); Insert into Charity values (13,'Henry Addington','10 Noorsekloof Road','Graiguenamanagh','Carlow','help@Henry Addington.ie','www.HenryAddington.ie',5000,2001,'Health'); Insert into Charity values (14,'William Pitt','1 Coastal House','Castlefin','Donegal','help@William Pitt.ie','www.WilliamPitt.ie',3000,2003,'Education');

Page 12: Agnieszka_Roguska_CharitySchoolEvents

11

Agnieszka Roguska A00188976

Insert into Charity values (15,'William Wyndham Grenville','Oosterland Straat 15','Blarney','Cork',null,'www.WilliamWyndhamGrenville.ie',4000,2005,'Famine Relief'); Insert into Charity values (16,'William Henry Cavenish Bentinck','No 6 Bayview Medical Centre','Banagher','Offaly',null,'www.WilliamHenryCavenishBentinck.ie',5000,2007,'Poverty'); Insert into Charity values (17,'Spencer Perceval','Alabama Centre','Crookstown','Cork','help@Spencer Perceval.ie','www.SpencerPerceval.ie',6000,2009,'Health'); Insert into Charity values (18,'Robert Banks Jenkinson','Medicentre','Ballyroe','Kildare','help@Robert Banks Jenkinson.ie','www.RobertBanksJenkinson.ie',7000,2011,'Education'); Insert into Charity values (19,'George Canning','385 Kempston Road','Greencastle','Donegal','help@George Canning.ie','www.GeorgeCanning.ie',8000,2013,'Famine Relief'); Insert into Charity values (20,'Robert Peel','2 Korsten Plaza','Clashmore','Waterford','help@Robert Peel.ie','www.RobertPeel.ie',9000,2001,'Poverty'); /* Table Name:Host */ /*Host_ID,Fname,Sname,Address,Town,County,Date_Of_Birth,Telephone_No,Mobile_No,Specialism */ Insert into Host values (100,'Larry','McGrath','4 Rodney Street','Grangemore','Brownstown','02-May-87','35343562545','353868814182','Radio'); Insert into Host values (101,'Peter','Brien','4804 Maronga Street','Collon','Glenealy','01-Jun-87','35343562542','353868814179','TV'); Insert into Host values (102,'Hubert','Donnelly','76 Cathcart Road','Galway City','Ballinlough','01-Jul-87','35343562546','353868814176','Sport'); Insert into Host values (103,'Arron','Donoghue','70 Manesi ','Cloonboo','Ballydesmond','31-Jul-87','35343562543','353868814173','Politics'); Insert into Host values (104,'Ronan','Dowling','68 Njoli Street','Ballymore Eustace','Ballygar','30-Aug-87',null,'353868814170','Radio'); Insert into Host values (105,'Charles','Kelly','Summerstrand Village','Golden','Drimoleague','29-Sep-87','35343562544','353868814167','TV'); Insert into Host values (106,'Dermot','McDonald','2 Church Street','Grangemore','Brownstown','29-Oct-87','35343562548','353868814164','Sport'); Insert into Host values (107,'Barry','Morrissey','86 Market Street Shop 13 A','Collon','Glenealy','28-Nov-87','35343562545','353868814161','Politics'); Insert into Host values (108,'Brian','Nevin','84 Constitution Road','Galway City','Ballinlough','28-Dec-87','35343562549','353868814158','Radio'); Insert into Host values (109,'Pauric','Quinlan','7 Chase Street','Cloonboo','Ballydesmond','27-Jan-88','35343562546','353868814155','TV'); Insert into Host values (110,'Niall','Brennan','Uitenhage','Ballymore Eustace','Ballygar','27-Mar-88',null,'353868814152','Sport'); Insert into Host values (111,'Barry','McGrath','34 Graaff Reinet Road','Golden','Drimoleague','26-May-88','34344523359','353868814149','Politics'); Insert into Host values (112,'Mark','Kavanagh','1st Floor Middestad Centre','Grangemore','Brownstown','25-Jul-88','34344523363','353868814146','Radio'); Insert into Host values (113,'Brendan','Kiernan','Baysvillage Shopping Centre','Collon','Glenealy','23-Sep-88',null,'353868814143','TV'); Insert into Host values (114,'Brian','Nolan','Hoek van Harts','Galway City','Ballinlough','22-Nov-88','34344523371','353868814140','Sport');

Page 13: Agnieszka_Roguska_CharitySchoolEvents

12

Agnieszka Roguska A00188976

Insert into Host values (115,'Pauric','Collins','1124 De Villiers Street','Cloonboo','Ballydesmond','21-Jan-89','34344523375','353868814137','Politics'); Insert into Host values (116,'Niall','Donohoe','Winkel 4','Ballymore Eustace','Ballygar','22-Mar-89','34344523379','353868814134','Radio'); Insert into Host values (117,'Barry','Forde','24137 Brits Road','Golden','Drimoleague','21-May-89','34344523383','353868814131','TV'); Insert into Host values (118,'Mark','Jackson','2 Library House','Golden','Drimoleague','20-Jul-89','34344523387','353868814128','Sport'); Insert into Host values (119,'Brendan','McCarey','22 B Harvey Road','Burtonport','Ballycumber','18-Sep-89','34344523391','353868814125','Politics'); /* Table Name:Sponsor */ /*Sponsor_ID,Name,Address,Town,County,Year_Established,Email_Address,Web_Site,Business_Type ,Num_Employees,Annual_Turnover */ Insert into Sponsor values (10,'Earl Russell','7 Shoprite Centre','Clonegal',' Carlow ',2013,'info@Earl_Russell.ie',null,'Manufacture',1200,10000); Insert into Sponsor values (11,'Edward George Stanley','Leadwood House','Dublin City',' Dublin',2010,'info@Edward_Stanley.ie',null,'Finance ',1080,8800); Insert into Sponsor values (12,'George Hamilton Gordon','Beacon Bay Retail Park','Garristown',' Fingal',2007,'info@George_Hamilton.ie','www.Georgordon.ie','Grocery',1201,10001); Insert into Sponsor values (13,'Henry John Temple','5 Berea Mall','Broadford',' Limerick',2004,'info@Henry_John_Temple.ie','www.Henemple.ie','Public House',1081,8801); Insert into Sponsor values (14,'John Stanley','Suite G11 Musgrave Park','Adare',' Limerick',2001,'info@John_Stanley.ie','www.John_Stanley.ie','Manufacture',1202,null); Insert into Sponsor values (15,'Henry Temple','Shop 4 Bungalow Mall','Ballyjamesduff',' Cavan',1998,'info@Henry_Temple.ie','www.Henry_Temple.ie','Finance ',1082,8802); Insert into Sponsor values (16,'Earl Russell','27 A King Street','Clonegal',' Carlow ',1995,'info@Earl_Russell.ie','www.Earl_Russell.ie','Grocery',1203,10003); Insert into Sponsor values (17,'Edward George Stanley','4 Byron Street','Dublin City',' Dublin',1992,'info@Edward_George.ie','www.Edwardanley.ie','Public House',1083,8803); Insert into Sponsor values (18,'Benjamin Disraeli','Dutywa Medical Centre','Garristown',' Fingal',1989,'info@Benjamin_Disraeli.ie','www.Benjamin_Disraeli.ie','Manufacture',200,10000); Insert into Sponsor values (19,'William Ewart Gladstone','1 Ludidi Building','Broadford',' Limerick',1986,'[email protected]','www.Wiadstone.ie','Finance ',210,null); Insert into Sponsor values (20,'Benjamin Disraeli','89 King Road','Adare',' Limerick',1983,'help@Benjamin_Disraeli.ie','www.Benjamin_Disraeli.ie','Grocery',220,10001); Insert into Sponsor values (21,'William Ewart Gladstone','33 King Street','Ballyjamesduff',' Cavan',1980,'[email protected]','www.Williamladstone.ie','Public House',230,8801); Insert into Sponsor values (22,'John White','The Colcade','Clonegal',' Carlow ',1977,'help@John_White.ie','www.John_White.ie','Manufacture',240,null); Insert into Sponsor values (23,'Kate Ewart Gladstone','5 Main Street','Dublin City',' Dublin',2001,'[email protected]','www.Kate_adstone.ie','Finance ',120,8802); Insert into Sponsor values (24,'Robert Arthur Talbot ','Vincent Street','Garristown',' Fingal',2003,'help@Robert_Arthurl.ie','www.Ro_Cecil.ie','Grocery',1207,10003);

Page 14: Agnieszka_Roguska_CharitySchoolEvents

13

Agnieszka Roguska A00188976

Insert into Sponsor values (25,'William Gladstone','Dublin road','Broadford',' Limerick',2005,'help@William_Gladstone.ie','www.Willistone.ie','Public House',1087,8803); Insert into Sponsor values (26,'Archibald Philip Primrose','29 Albany Street','Adare',' Limerick',2007,'[email protected]','www.Archibmrose.ie','Manufacture',1208,10004); Insert into Sponsor values (27,'Robert Arthur ','Sibaya Sempilo Medical','Ballyjamesduff',' Cavan',2009,'[email protected]','www.Talbotcil.ie','Finance ',1088,8804); Insert into Sponsor values (28,'Arthur James Balfour ','45 Oriental Plaza','Donegal',' Donegal',2011,'help@Arthuour_.ie','www.Arthlfour_.ie','Grocery',1209,10005); Insert into Sponsor values (29,'Henry Bannerman','Main Road 8','Ardagh',' Limerick',2013,'help@Henry_Bannerman.ie','www.Henry_Bannerman.ie','Public House',1089,8805); /* Table Name:Patron */ /*Patron_Id,Name,Address,Town,County,Phone_No,Email_Add,Web_Site */ Insert into Patron values (301,'Mercy Sisters','95 BLOOMFIELD ROAD','Drumlish','Longford','35343456544','info@Mercy_Sisters.ie','www.Mercy_Sisters.ie'); Insert into Patron values (302,'Christian Brothers','120 ORMEAU ROAD','Carrickmacross','Monaghan','35343456540','info@Christian_Brothers.ie','www.Christian_Brothers.ie'); Insert into Patron values (303,'Church Brothers','216 GROSVENOR ROAD','Annascaul','Kerry','35343456545','info@Church_Brothers.ie','www.Church_Brothers.ie'); Insert into Patron values (304,'Council Longford','74A LIGONIEL ROAD','Clonaslee','Cork','35343456541','info@Council_Longford.ie','www.Council_Longford.ie'); Insert into Patron values (305,'Christan Home','216 GROSVENOR ROAD','Clonaslee','Longford','35343456546','info@Christan_Home.ie','www.Christan_Home.ie'); Insert into Patron values (306,'Angel Wings','LINENHALL STREET','Ballyclerahan','Tipperary','35343456542','info@Angel_Wings.ie','null'); Insert into Patron values (307,'Brothers ','68-72 NEWTOWNARDS ROAD','Cavan','Cavan',null,'info@Brothers_.ie','www.Brothers_.ie'); Insert into Patron values (308,'Mercy Church','LINENHALL STREET','Belmullet','Mayo','35344854555','info@Mercy_Church.ie','www.Mercy_Church.ie'); Insert into Patron values (309,'Offaly VEC','Water Road 3 ','Drumlish','Longford','35344854561','info@Offaly_VEC.ie','null'); Insert into Patron values (310,'Longford VEC','179 ANDERSONSTOWN ROAD','Carrickmacross','Monaghan','35344854567','info@Longford_VEC.ie','www.Longford_VEC.ie'); Insert into Patron values (311,'Weastmeath VEC','13/25 FINAGHY ROAD SOUTH','Annascaul','Kerry','35344854573','help@Weastmeath_VEC.ie','www.Weastmeath_VEC.ie'); Insert into Patron values (312,'Offaly VEC','169 UPPER NEWTOWNARDS ROAD','Clonaslee','Cork','35344854579','help@Offaly_VEC.ie','www.Offaly_VEC.ie');

Page 15: Agnieszka_Roguska_CharitySchoolEvents

14

Agnieszka Roguska A00188976

Insert into Patron values (313,'Dublin Vec','Water Road 6','Clonaslee','Longford','35344854585','help@Dublin_Vec.ie','www.Dublin_Vec.ie'); Insert into Patron values (314,'Dublin Council','13/25 FINAGHY ROAD SOUTH','Ballyclerahan','Tipperary','35344854591','help@Dublin_Council.ie','www.Dublin_Council.ie'); Insert into Patron values (315,'Mary','Dublin Road','Carrickmacross','Monaghan',null,'[email protected]','www.Mary.ie'); Insert into Patron values (316,'Adult education Centre Dublin','13/25 FINAGHY ROAD SOUTH','Annascaul','Kerry','35344854603','help@Adult_education_Centre_Dublin.ie','www.Adult_education_Centre_Dublin.ie'); Insert into Patron values (317,'Fas Longford','3 KILLAUGHEY ROAD','Ballydehob','Cork','35344854609','help@Fas__Longford.ie','www.Fas__Longford.ie'); Insert into Patron values (318,'Christan Sisters','68-72 NEWTOWNARDS ROAD','Clonaslee','Laois',null,'help@Christan_Sisters.ie','www.Christan_Sisters.ie'); Insert into Patron values (319,'Athlone VEC','Main Street 4','Ballyclerahan','Tipperary','35344854621','help@Athlone_VEC.ie','www.Athlone_VEC.ie'); Insert into Patron values (320,'John White','179 ANDERSONSTOWN ROAD','Cavan','Cavan','35344854627','help@John_White.ie','www.John_White.ie'); /* Table Name:School */ /*School_ID,Name,Address,Town,County,Num_Students,Email_Add,Web_Site,Phone_No,Category ,Gender ,Patron_Id */ Insert into School values (201,'Moneystowns','309 Grosvenor Road','Brownstown','Kildare',25633,'[email protected]','www.Moneystowns.ie','434445225','Secondary','Mixed',301); Insert into School values (202,'Kiltegans','69 Shankill Road','Glenealy','Wicklow',5425,'[email protected]','www.Kiltegans.ie','434445221','Community','Boys',302); Insert into School values (203,'Carysfort ','225 Albertbridge ','Ballinlough','Roscommon',455,'info@Carysfort .ie','www.Carysfort .ie','434445226','Vocational','Girls',304); Insert into School values (204,'Caoimhin','1B Drumart Square','Ballydesmond','Cork',4000,'[email protected]','www.Caoimhin.ie','434445222','Secondary','Mixed',305); Insert into School values (205,'Cronan ','257 North Queen ','Ballygar','Galway',4130,'info@Cronan .ie','www.Cronan .ie',null,'Community','Boys',306); Insert into School values (206,'Clochar ','423B Upper Newto','Drimoleague','Cork',4001,'contact@Clochar .ie','www.Clochar .ie','434445223','Vocational','Girls',307); Insert into School values (207,'Padraig','Water Street 45','Ballycumber','Offaly',4131,'[email protected]','www.Padraig.ie','445656565','Secondary','Mixed',308); Insert into School values (208,'Naomhpadraig','68-72 Newtow','Gowran','Kilkenny',8000,'[email protected]','www.Naomhpadraig.ie','445656568','Community','Boys',309); Insert into School values (209,'Choroin ','Main Street 67','Ballyconnell','Cavan',2560,'info@Choroin .ie','www.Choroin .ie','445656571','Vocational','Girls',310);

Page 16: Agnieszka_Roguska_CharitySchoolEvents

15

Agnieszka Roguska A00188976

Insert into School values (210,'Ballintemple','1 Dunluce Avenue','Ballyhaise','Cavan',1365,'[email protected]','www.Ballintemple.ie','445656574','Secondary','Mixed',311); Insert into School values (211,'Carnew','New Road 6','Castlemartyr','Cavan',170,'[email protected]','www.Carnew.ie',null,'Community','Boys',312); Insert into School values (212,'Kierans','Main Road 4','Edenderry','Offaly',2562,'[email protected]','www.Kierans.ie','445656580','Vocational','Girls',301); Insert into School values (213,'Catherines','14 Downpatrick ','Johnstown','Kilkenny',1995,'[email protected]','www.Catherines.ie','445656583','Secondary','Mixed',314); Insert into School values (214,'Marino','Belfast Road','Bridge End','Donegal',2563,'[email protected]','www.Marino.ie','445656586','Community','Boys',315); Insert into School values (215,'Naird','Newtownards Road','Ballisodare','Sligo',1996,'[email protected]','www.Naird.ie','445656589','Vocational','Girls',316); Insert into School values (216,'Gearard','Bangor','Glasslough','Monaghan',2564,'[email protected]','www.Gearard.ie','445656592','Secondary','Mixed',317); Insert into School values (217,'Greystones','1 Dunluce Avenue','Glenamaddy','Galway',1997,'[email protected]','www.Greystones.ie','445656595','Community','Boys',318); Insert into School values (218,'Blessington','Main Street 33','Ballytore','Kildare',56655,'[email protected]','www.Blessington.ie','445656598','Vocational','Girls',319); Insert into School values (219,'Brayshool','216 Grosvenor Road','Abbeyfeale','Limerick',7741,'[email protected]','www.Brayshool.ie','445656601','Secondary','Mixed',320); Insert into School values (220,'Peters','Rathnew','Emyvale','Monaghan',1254,'[email protected]','www.Peters.ie','445656604','Community','Boys',320); /* Table Name:Event */ /* Event_Id,Description,Event_Date,Target_Amount ,Actual_Amount ,Start_Time,End_Time,Total_Raised,Charity_Id,School_ID,Spot_Prize_Value,Sponsor_ID,Host_ID*/ Insert into Event values (101,'Auction','11-Jan-13',10000,1200,8,13,10000,1,206,12000,10,100); Insert into Event values (102,'Cakes sale','12-Nov-12',9757,11657,9,14,9000,2,207,11868,11,101); Insert into Event values (103,'Auction','12-Sep-13',10001,12001,8,15,8000,3,208,12001,12,100); Insert into Event values (104,'Clothes sale','24-Jun-13',9758,11658,10,16,7000,4,209,11869,13,103); Insert into Event values (105,'Cakes sale','13-Dec-13',10002,12002,11,17,6000,10,210,12002,14,104);

Page 17: Agnieszka_Roguska_CharitySchoolEvents

16

Agnieszka Roguska A00188976

Insert into Event values (106,'Charity basketball','14-Oct-13',9759,11659,8,18,5000,6,211,11870,15,105); Insert into Event values (107,'Clothes sale','14-Sep-13',10003,12003,9,19,4000,7,212,12003,16,106); Insert into Event values (108,'Cakes sale','16-Jul-13',9760,11660,8,13,3000,8,213,12003,14,107); Insert into Event values (109,'Auction','15-Jan-13',10004,12004,7,14,2000,9,214,11871,14,107); Insert into Event values (110,'Clothes sale','07-Oct-12',9761,12003,12,15,1000,20,210,12004,15,109); Insert into Event values (111,'Bingo','16-Sep-13',10005,11660,9,13,2000,5,211,12004,16,110); Insert into Event values (112,'Auction','18-Jul-13',10005,12004,9,14,2100,6,212,11872,17,111); Insert into Event values (113,'Clothes sale','17-Sep-13',9762,11661,8,15,2200,7,213,12005,18,112); Insert into Event values (114,'Cakes sale','19-Jul-13',10006,12005,9,16,2300,8,214,12005,10,113); Insert into Event values (115,'Auction','18-Feb-13',10006,12004,9,17,2400,9,215,11873,24,105); Insert into Event values (116,'Bingo','20-Dec-12',9763,11661,8,18,2500,10,216,12006,25,106); Insert into Event values (117,'Cakes sale','19-Sep-13',10005,12005,9,19,2600,11,217,12005,26,107); Insert into Event values (118,'Auction','21-Jul-13',9762,11662,8,14,2700,18,218,12005,27,117); Insert into Event values (119,'Clothes sale','20-May-13',10005,12006,9,15,2800,19,219,11873,10,118); Insert into Event values (120,'Cakes sale','21-Mar-13',10005,12005,9,16,2900,20,220,12005,29,119);

Page 18: Agnieszka_Roguska_CharitySchoolEvents

17

Agnieszka Roguska A00188976

6. Tables

Page 19: Agnieszka_Roguska_CharitySchoolEvents

18

Agnieszka Roguska A00188976

Page 20: Agnieszka_Roguska_CharitySchoolEvents

19

Agnieszka Roguska A00188976

7. Single Table Queries

1. Show all Charities which were founded after 1978 in a City containing

letter “a “ , with a Mission : Health, Education or Poverty with no Email

address and Annual Budget Between 4000 and 350000

Select Name,Annual_Budget,City,Mission,Email, Yr_Founded

From Charity

Where Yr_Founded > 1978

And City Like ’%a%’

And Mission In(‘Health’,’Education’,’Poverty’)

And Annual_Budget Between 4000 And 350000

And Email IS Null;

Page 21: Agnieszka_Roguska_CharitySchoolEvents

20

Agnieszka Roguska A00188976

Qualified Column Names

Select Charity.Name, Charity.Annual_Budget, Charity.City,

Charity.Mission, Charity.Email, Charity.Yr_Founded

From Charity

Where Charity.Yr_Founded > 1978

And Charity.City Like ’%a%’

And Charity.Mission In(‘Health’,’Education’,’Poverty’)

And Charity.Annual_Budget Between 4000 And 350000

And Charity.Email IS Null;

2. Show all Hosts which were born before 02 may 1990 in town which

first letter is not “C” , with Host Id number not between 102 and 119

and no Telephone number.

Select Host_Id,Date_Of_Birth,Town, Telephone_No

From Host

Where Date_Of_Birth <’02-May-90’

And Town Not Like ‘C_%’

And Host_ID Not Between 102 And 109

And Telephone_No IS NOT NULL;

Page 22: Agnieszka_Roguska_CharitySchoolEvents

21

Agnieszka Roguska A00188976

Qualified Column Names

Select Host.Host_Id, Host.Date_Of_Birth, Host.Town, Host.Telephone_No

From Host

Where Host.Date_Of_Birth <’02-May-90’

And Host.Town Not Like ‘C_%’

And Host.Host_ID Not Between 102 And 109

And Host.Telephone_No IS NOT NULL;

Page 23: Agnieszka_Roguska_CharitySchoolEvents

22

Agnieszka Roguska A00188976

3. Show all Sponsors from County Dublin which lives in Adare, Dublin City

or Clonegal with a Name starting with letter” E” , with Sponsor Id in

Ascending Order

Select Name,Town, Sponsor_Id, County as Co

From Sponsor

Where County = ‘Dublin’

And Town In(’Dublin City’,’Adare’,’Clonegal’)

And Name Like ’E_%’

Order By Sponsor_Id Asc;

Page 24: Agnieszka_Roguska_CharitySchoolEvents

23

Agnieszka Roguska A00188976

Qualified Column Names

Select Sponsor.Name, Sponsor.Town, Sponsor.Sponsor_Id,

Sponsor.County as Co

From Sponsor

Where Sponsor.County = ‘Dublin’

And Sponsor.Town In(’Dublin City’,’Adare’,’Clonegal’)

And Sponsor.Name Like ’E_%’

Order By Sponsor.Sponsor_Id Asc;

4. Show all patrons in Descending order, with town in upper case and

Email Address in lower case not from County Longford and show the

number of character in Name

Select Length(Name) As Name ,Upper(Town)As

Town,Lower(Email_Add)As Email

From Patron

Where County<> ‘Longford’

Order By Patron_Id Desc;

Page 25: Agnieszka_Roguska_CharitySchoolEvents

24

Agnieszka Roguska A00188976

Qualified Column Names

Select Length(Patron.Name) As Name , Upper(Patron.Town)As

Town,Lower(Email_Add)As Email

From Patron

Where Patron.County<> ‘Longford’

Order By Patron.Patron_Id Desc;

5. Show all Schools with Id number lower or equal to 219 , with a

address not containing letter a .Also show the Email address without last

letters “.ie”, and Website Address without fist letters “www.”

Page 26: Agnieszka_Roguska_CharitySchoolEvents

25

Agnieszka Roguska A00188976

Select School_ID,Address as Ad, Initcap(Name),

Ltrim(Web_Site,’www.’), Rtrim(Email_Add,’.ie’)

From School

Where School_ID <=219

And Address Not Like ’%a%’;

Qualified Column Names

Select School. School_ID, School. Address as Ad, Initcap(School.

Name), Ltrim(School. Web_Site,’www.’), Rtrim(School.

Email_Add,’.ie’)

From School

Where School. School_ID <=219

And School. Address Not Like ’%a%’;

Page 27: Agnieszka_Roguska_CharitySchoolEvents

26

Agnieszka Roguska A00188976

8. Joins

Join Table Query Using School and Event

Page 28: Agnieszka_Roguska_CharitySchoolEvents

27

Agnieszka Roguska A00188976

1. Show all the names of Schools that contain letter “a”, that are

from county Cork,Cavan,Monaghan,Galway with Target

Amount between 5 and 500000 . Order by Name.

Select Name, School.County,Target_Amount

From School,Event

Where School.School_ID=Event.School_ID

And Name Like ‘%a%’

And School.County IN (‘Cork’,’Cavan’,’ Monaghan’,’Galway’)

And Target_Amount BETWEEN 5 And 500000

ORDER By Name ;

QUALIFIED COLUMN NAMES

Select School.Name, School.County,Event.Target_Amount

From School,Event

Where School.School_ID=Event.School_ID

And School.Name Like ‘%a%’

And School.County IN (‘Cork’,’Cavan’,’ Monaghan’,’Galway’)

And Event.Target_Amount BETWEEN 5 And 500000

Page 29: Agnieszka_Roguska_CharitySchoolEvents

28

Agnieszka Roguska A00188976

ORDER By School.Name ;

2. Show the names of the Schools with the Town not containg letter “z”,

with Host Surname in Upper Case with Target Amount not between 100

to 225.Order by Surname

Select Upper(Sname) As Surname, Name,Target_Amount As Target

From Host,School ,Event

Where Host.Host_Id=Event.Host_Id

And School.School_Id=Event.School_Id

And School.Town Not Like ‘%z%’

Page 30: Agnieszka_Roguska_CharitySchoolEvents

29

Agnieszka Roguska A00188976

And Target_Amount Not Between 100 AND 250

Order By Sname;

TABLE ALIAS

Select Upper(H.Sname) As Surname, S.Name,E.Target_Amount As Target

From Host H,School S,Event E

Where H. Host_Id=E.Host_Id

And S.School_Id=E.School_Id

And S.Town Not Like ‘%z%’

And E.Target_Amount Not Between 100 AND 250

Order By H.Sname;

Page 31: Agnieszka_Roguska_CharitySchoolEvents

30

Agnieszka Roguska A00188976

Page 32: Agnieszka_Roguska_CharitySchoolEvents

31

Agnieszka Roguska A00188976

3. Show For Each Charity the Id number and Name with the Id

number of host and Town they are from.Also List the Sponsor Name

and Business Type .Include All Schools from county Kildare and Cavan

.Order By Charity name

Select

C.Charity_Id,C.Name,H.Host_Id,H.Town,S.Name,S.Business_Type,P.Name,P.Town,SL.N

ame,

SL.County,E.Description,E.Event_Date,E.Target_Amount As Target

,E.Actual_Amount

From Charity C,Host H,Sponsor S,Patron P,School SL,Event E

Where C.Charity_Id=E.Charity_Id

And H.Host_ID=E.Host_ID

And S.Sponsor_ID=E.Sponsor_ID

And P.Patron_Id=SL.Patron_Id

And SL.School_ID =E.School_ID

And SL.County IN(‘Kildare’,’Cavan’)

Order By C.Name;

Page 33: Agnieszka_Roguska_CharitySchoolEvents

32

Agnieszka Roguska A00188976

4. Show All Patron Id which number of letters in the County happens

to be this same as the number of letters in School County, with School Category different than Community or Secondary. Also the Address of

School should be in Upper Case with Website address in Lower.

An Equijoin using Patron and School Table

Select

Patron.Patron_Id,Length(Patron.County),Length(School.County),

School_Id ,School.Name,Upper

(School.Address),Lower(School.Web_Site),School.Category

From Patron, School

Where Length(School.County) = Length(Patron.County)

And School.Category Not IN (‘Community’,’Secondary’);

TABLE ALIAS

Select P.Patron_Id,Length(P.County),Length(S.County),S.School_Id

,S.Name,Upper (S.Address),Lower(S.Web_Site),S.Category

From Patron P, School S

Where Length(S.County) = Length(P.County)

And S.Category Not IN (‘Community’,’Secondary’);

Page 34: Agnieszka_Roguska_CharitySchoolEvents

33

Agnieszka Roguska A00188976

5. Show all the Charities and Events .Set all Charities mission

to starts with capital letter with the Annual Turnover as well as half

of Annual budget. Also show Event Description with Actual Amount

which is bigger or equal to 1200, with Spot prize not equal null . Show

Only the result where Charity Name length is smaller than the length of

Description in Event An Non-Equijoin using Charity and Event Table

Select Charity.Name,Initcap(Mission),Annual_Budget As Budget,

Annual_Budget/2,Event_Id,Event.Description,Actual_Amount,Spot_Priz

e_Value As Spot_Prize

From Charity,Event

Where Length(Charity.Name) < Length(Event.Description)

And Actual_Amount >=1200

And Spot_Prize_Value IS NOT NULL;

TABLE ALIAS

Page 35: Agnieszka_Roguska_CharitySchoolEvents

34

Agnieszka Roguska A00188976

Select C.Name,Initcap(C.Mission),C.Annual_Budget As Budget,

C.Annual_Budget/2,E.Event_Id,E.Description,E.Actual_Amount,E.Spot_P

rize_Value As Spot_Prize

From Charity C,Event E

Where Length(C.Name) < Length(E.Description)

And E.Actual_Amount >=1200

And E.Spot_Prize_Value IS NOT NULL;

6. Show the Name And Annual Turnover of sponsors with identical

Annual Turnover and Year established before 2006. Order All by Name

Asceding

A Self join Query using SponsorTable

Select S1.Sponsor_Id,S1.Name, S1.Annual_Turnover, S1.Year_Established,

S2.Sponsor_Id,S2.Name, S2.Annual_Turnover, S2.Year_Established

From Sponsor S1, Sponsor S2

Where S1.Year_Established < 2006

And S1.Sponsor_Id > S2.Sponsor_Id

And S1. Annual_Turnover = S2. Annual_Turnover

Order By S1.Name ASC;

Page 36: Agnieszka_Roguska_CharitySchoolEvents

35

Agnieszka Roguska A00188976

7.Show the Patron and School Ids , Include whose Hosts which are not

affialiated with any schools .Also show the school gender .Show

Patron_Id as Patron .Order by Patron Id Desceding

An Outer join using the Patron and School Table

Select Patron.Patron_Id As Patron ,Patron.Name,School_Id,School.Name,Gender

From Patron, School

Where Patron.Patron_Id=School.Patron_Id(+)

Order BY Patron.Patron_Id Desc;

Page 37: Agnieszka_Roguska_CharitySchoolEvents

36

Agnieszka Roguska A00188976

TABLE ALIAS

Select P.Patron_Id As Patron ,P.Name,S.School_Id,S.Name,S.Gender

From Patron P, School S

Where P.Patron_Id=S.Patron_Id(+)

Order BY P.Patron_Id Desc;

Spool OFF

9. Summary Queries

1. Show the Schools with the maximum and minimum Number of

Students. Show Only schools which contain letter ’a’ choose only

from girls or boys Schools

Select Max(S. Num_Students), Min(S. Num_Students)

From School S

Where S.County Like ‘%a%’

And S.Gender In (‘Girls’, ‘Boys’);

Page 38: Agnieszka_Roguska_CharitySchoolEvents

37

Agnieszka Roguska A00188976

2. Show the average and sum for all Target Amounts for Events as

well as the count. Don’t include Description containing letter “z”

and don’t show the Actual Amount between 122000 and 850005.

Select Avg(E. Target_Amount), Sum(E. Target_Amount),

Count(E. Target_Amount)

From Event E

Where E.Description Not Like (‘%z%’)

And E. Actual_Amount Not Between 122000 And 850005;

3. Show all Patrons with no phone number. Show Patron Id as Id

Page 39: Agnieszka_Roguska_CharitySchoolEvents

38

Agnieszka Roguska A00188976

Select count(*), Count(P.Patron_Id) As Id

From Patron P

Where P.Phone_No is null;

4. Show the average number of students where there is no phone

number , also show the length of the county.

Select Avg(S. Num_Students),Length(S.County)

From School S

Where S.Phone_No is null;

5. Name the highest and lowest target amounts and the difference

between them. Show target amount as target. Also include Events

with total raised lesser than 1245 with a target amount between

10 and 12000

Page 40: Agnieszka_Roguska_CharitySchoolEvents

39

Agnieszka Roguska A00188976

Select Max(Target_Amount), Min(Target_Amount),

Max(Target_Amount) – Min (Target_Amount) Target

From Event E

Where Total_Raised <= 12456

And Target_Amount Between 10 And 12000;

6. Show the highest, lowest and average number of students. Group

and order them by Gender.

Select Max(S.Num_Students), Min (S.Num_Students),

Avg(S.Num_Students),S.Gender

From School S

Group By S.Gender

Order By Gender;

7. Name all Schools with Average Event target amount lesser than

12345. Group by Gender. Order by Gender.

Select S.Gender, Avg(E.Target_Amount)

From School S, Event E

Page 41: Agnieszka_Roguska_CharitySchoolEvents

40

Agnieszka Roguska A00188976

Where S.School_Id = E.School_Id

And E.Target_Amount < 12345

Group By S.Gender

Order By S.Gender;

Page 42: Agnieszka_Roguska_CharitySchoolEvents

41

Agnieszka Roguska A00188976

8. Show the highest, lowest and average total raised of Events.

Group the description having the minimum total raised greater

than 80. Call the minimum target as Total.

Order by description.

Select E.Description, Max(E.Total_Raised), Min(E.Total_Raised)As

Total

From Event E

Group By E.Description

Having Min(E.Total_Raised) > 80

Order By E.Description;

Page 43: Agnieszka_Roguska_CharitySchoolEvents

42

Agnieszka Roguska A00188976

9. Show the sum and average of Number of students. Show only the

schools with number of students between 10 and 9900. Group by

Gender type, having the count of Number of students exceed 1.

Order by Gender.

Select S.Gender, Count(S.Num_Students ), Sum(S.Num_Students

), Avg(S.Num_Students ),Max(

From School S, Patron P

Where S.Patron_Id = P.Patron_Id

And S.Num_Students Between 10 And 9900

Group By S.Gender

Having Count(S.Num_Students) > 1

Order By S.Gender;

Page 44: Agnieszka_Roguska_CharitySchoolEvents

43

Agnieszka Roguska A00188976

10. Subqueries

1. Show the highest Number of Employees whose business

type is Manufacture or Grocery. Show only where the

number of employees is between 100 and 1256000.

Select Name ,Business_Type,Num_Employees

From Sponsor

Where Business_Type IN (‘Manufacture’,’Grocery’)

And Num_Employees Between 100 And 1256000

And Num_Employees =

(Select Max(Num_Employees)

From Sponsor);

Page 45: Agnieszka_Roguska_CharitySchoolEvents

44

Agnieszka Roguska A00188976

2. Give the Name, County of the school with the highest

number of students. Only girls Gender .Do not Show

schools which begin with ‘M’

Select Name,County,Num_Students,Gender

From School

Where Gender = ‘Girls’

And Name Not Like (‘M%’)

And Num_Students =

(Select Max(Num_Students)

From School

Where Gender = ‘Girls’);

Page 46: Agnieszka_Roguska_CharitySchoolEvents

45

Agnieszka Roguska A00188976

3. Show all the Charities with Health mission Statement that

have a higher Annual_Budget than Charities with Education

mission Statement . Also show Name as School Name

Select Name as School_Name,Mission,Annual_Budget

From Charity

Where Mission = ‘Health’

And Annual_Budget >

(Select Max(Annual_Budget)

From Charity

Where Mission = ‘Education’);

Page 47: Agnieszka_Roguska_CharitySchoolEvents

46

Agnieszka Roguska A00188976

4. Show the Schools from Cavan that have the same gender as

those that are from Kildare . Only show those Schools with

Id number between 201 and 216

Select School_Id Name, County,Gender

From School

Where County = ‘Cavan’

And Gender IN

(Select Gender

From School

Where County = ‘Kildare’)

And School_Id Between 201 and 216;

Page 48: Agnieszka_Roguska_CharitySchoolEvents

47

Agnieszka Roguska A00188976

5. Show the Sponsors that have business type containing letter

“e”. Only show Auction Events

Select Sponsor_Id,Business_Type

From Sponsor

Where Business_Type Like (‘%e%’)

And Sponsor.Sponsor_Id IN

(Select Event.Sponsor_Id

From Event

Where Description = ‘Auction’);

Page 49: Agnieszka_Roguska_CharitySchoolEvents

48

Agnieszka Roguska A00188976

6. Show the Events of the Schools that are not mixed gender.

Order by Event Id

Select Event_Id ,Description

From Event

Where Event.Event_Id NOT IN

(Select School_Id

From School

Where Gender = ‘Mixed’)

Order By Event_Id;

Page 50: Agnieszka_Roguska_CharitySchoolEvents

49

Agnieszka Roguska A00188976

7. Show the names of Patrons with length of the county that

were educated in schools which have Events Description as

Auction.

Select Name, Length(County)

From Patron

Where Patron.Patron_Id IN

(Select Patron_Id

From School

Where School.School_Id IN

(Select School_Id

From Event

Where Description=‘Auction’));

Page 51: Agnieszka_Roguska_CharitySchoolEvents

50

Agnieszka Roguska A00188976

8. Show the Event Id ,Description with Target Amount less than

15000 Also show Schools with Patron Id lesser than 319 . Order

by School Id.

Select Event_Id, Description, Target_Amount

From Event

Where Target_Amount < 15000

And Event.School_Id IN

(Select School_Id

From School

Where School.Patron_Id In

(Select Patron_Id

From Patron

Where Patron_Id < 319))

Order By School_Id;

Page 52: Agnieszka_Roguska_CharitySchoolEvents

51

Agnieszka Roguska A00188976

9. Give the name and county of the Charity where the Patron of

the event is in Cork

Select Name, County

From Charity

Where Charity.Charity_Id IN

(Select Event.Charity_Id

From Event

Where Event.School_Id In

(Select School.School_Id

From School

Where School.Patron_Id IN

(Select Patron.Patron_Id

From Patron

Where County = ‘Cork’)));

Page 53: Agnieszka_Roguska_CharitySchoolEvents

52

Agnieszka Roguska A00188976

10. Give the name and county of the school where the

actual amount raised in the event is NOT more than 10000

Select Name, County

From School

Where School.School_id NOT IN

(Select Event.School_Id

From Event

Where Actual_Amount >= 10000);

Page 54: Agnieszka_Roguska_CharitySchoolEvents

53

Agnieszka Roguska A00188976

11.Update, Inserts, Deletes

Delete Queries

1 . Attempt to delete all Patrons with Id number 301.Demonstrate

that it doesn’t work.

Select Patron_id,Name,County

From Patron

Page 55: Agnieszka_Roguska_CharitySchoolEvents

54

Agnieszka Roguska A00188976

Where Patron_Id=301

Delete

From Patron

WHERE Patron_Id = 301;

Rollback;

2. Remove all schools that have not any events where Category of

school is Secondary.

Select S.School_ID, S.Name,S.Category

From School S

Where Category = ‘Secondary’

And S.School_ID NOT IN

(Select E.School_ID

From Event E);

Page 56: Agnieszka_Roguska_CharitySchoolEvents

55

Agnieszka Roguska A00188976

Delete

From School S

Where Category = ‘Secondary’

And S.School_ID NOT IN

(Select E.School_ID

From Event E);

Select S.School_ID, S.Name,S.Category

From School S

Where Category = ‘Secondary’

And S.School_ID NOT IN

(Select E.School_ID

From Event E);

Page 57: Agnieszka_Roguska_CharitySchoolEvents

56

Agnieszka Roguska A00188976

Rollback;

Select S.School_ID, S.Name,S.Category

From School S

Where Category = ‘Secondary’

And S.School_ID NOT IN

(Select E.School_ID

From Event E);

3.Delete all from Event table where the School Patron is located in Longford

Page 58: Agnieszka_Roguska_CharitySchoolEvents

57

Agnieszka Roguska A00188976

Select E.Description, E.Target_Amount

From Event E

Where E.School_ID IN

(Select S.School_ID

From School S

Where S.Patron_Id IN

(Select Patron_Id

From Patron

Where County = ‘Longford’));

Delete

From Event E

Where E.School_ID IN

(Select S.School_ID

From School S

Where S.Patron_Id IN

(Select Patron_Id

From Patron

Where County = ‘Longford’));

Select E.Description, E.Target_Amount

From Event E

Where E.School_ID IN

(Select S.School_ID

From School S

Where S.Patron_Id IN

(Select Patron_Id

From Patron

Where County = ‘Longford’));

Rollback;

Select E.Description, E.Target_Amount

From Event E

Page 59: Agnieszka_Roguska_CharitySchoolEvents

58

Agnieszka Roguska A00188976

Where E.School_ID IN

(Select S.School_ID

From School S

Where S.Patron_Id IN

(Select Patron_Id

From Patron

Where County = ‘Longford’));

4. Delete from School where the Patron has more than 80 students

Select Name, County

Page 60: Agnieszka_Roguska_CharitySchoolEvents

59

Agnieszka Roguska A00188976

From School

Where School_ID IN

(Select School_ID

From Patron

Where Num_Students > 80);

Delete

From School

Page 61: Agnieszka_Roguska_CharitySchoolEvents

60

Agnieszka Roguska A00188976

Where School_ID IN

(Select School_ID

From Patron

Where Num_Students > 80);

Select Name, County

From School

Where School_ID IN

(Select School_ID

From Patron

Where Num_Students > 80);

Rollback;

Select Name, County

From School

Where School_ID IN

(Select School_ID

From Patron

Where Num_Students > 80);

Insert Queries 1.

Page 62: Agnieszka_Roguska_CharitySchoolEvents

61

Agnieszka Roguska A00188976

Insert into Sponsor (County,Sponsor_ID, Business_Type, Name) Values (‘Dublin’,

30, ‘Finace’, ‘Jason Staton’);

Select *

From Sponsor

Where Sponsor_ID = 30;

Rollback;

Select *

From Sponsor

Where Sponsor_ID = 30;

2.

Page 63: Agnieszka_Roguska_CharitySchoolEvents

62

Agnieszka Roguska A00188976

Insert into School(School_ID, Name, Address, Town, County) Values (221,

‘Michaels’, Null, Null, ‘Kildare’);

Select School_Id, Name, Address, Town, County

From School

Where School_Id = 221;

Rollback;

Select School_Id, Name, Address, Town, County

From School

Where School_Id = 221;

Page 64: Agnieszka_Roguska_CharitySchoolEvents

63

Agnieszka Roguska A00188976

3. Insert into School (School_ID, Name, Category, Patron_Id) VALUES

(221, ‘Colins’, ‘Community’, 322);

Insert into School (School_ID, Name, Category, Patron_Id) VALUES (321,

‘Michaels’, ‘Secondary’, 301);

Select School_ID, Name, Patron_Id

From School

Where Patron_Id = 301;

Rollback;

Select School_ID, Name, Patron_Id

From School

Where Patron_Id = 301;

Page 65: Agnieszka_Roguska_CharitySchoolEvents

64

Agnieszka Roguska A00188976

Page 66: Agnieszka_Roguska_CharitySchoolEvents

65

Agnieszka Roguska A00188976

UPDATE QUERIES 1.Update any Patrons with the School ID of 301 changing the School Id numbers

to 321

Select School_Id, Name, Patron_Id

From School

Where Patron_Id = 301;

Update School

Set Patron_Id = 321

Where Patron_Id = 301;

Update School

Set Patron_Id = 304

Where Patron_Id = 301;

Select School_ID, Name, Patron_Id

From School

Where Patron_Id IN (301, 304);

Page 67: Agnieszka_Roguska_CharitySchoolEvents

66

Agnieszka Roguska A00188976

Rollback;

Select School_ID, Name, Patron_Id

From School

Where Patron_Id IN (301, 304);

2.Update Patron Id 302 to 600

Select Patron_Id, Name

From Patron

Where Patron_Id = 302;

Page 68: Agnieszka_Roguska_CharitySchoolEvents

67

Agnieszka Roguska A00188976

Update Patron

Set Patron_Id = 600

Where Patron_Id = 302;

Page 69: Agnieszka_Roguska_CharitySchoolEvents

68

Agnieszka Roguska A00188976

3.Delete all rows from the School table and count how many rows are deleted

over the whole database

Select Count(*)

From Charity;

Select Count(*)

From Host;

Select Count(*)

From Sponsor;

Select Count(*)

From Patron;

Select Count(*)

From School;

Select Count(*)

From Event;

Page 70: Agnieszka_Roguska_CharitySchoolEvents

69

Agnieszka Roguska A00188976

Delete

From Charity;

Select Count(*)

From Charity;

Select Count(*)

From Host ;

Select Count(*)

From Sponsor;

Select Count(*)

From Patron;

Select Count(*)

From School;

Select Count(*)

From Event;

I add the “on delete cascade “in school table but its not letting me in Event table

that’s why I created again table Event but without “on delete cascade”

Page 71: Agnieszka_Roguska_CharitySchoolEvents

70

Agnieszka Roguska A00188976

11. Views

1. Create Vertical View which will show all Sponsors from Manufacture business

Create View Sponsor_Manufacture As Select Sponsor_Id,Name,Town,County,Business_Type

From Sponsor

Where Business_Type =’Manufacture’;

2. Create a horizontal View in which Annual Turnover will be doubled in Grocery

Business Type

Page 72: Agnieszka_Roguska_CharitySchoolEvents

71

Agnieszka Roguska A00188976

Create View Sponsor_Grocery_Doubled As

Select*

From Sponsor

Where Business_Type = ’Grocery’

And Year_Established > 1960 ;

Select*

From Sponsor_Grocery_Doubled ;

Select Annual_turnover*2 As Double_AnnualTurnover

From Sponsor_Grocery_Doubled ;

Page 73: Agnieszka_Roguska_CharitySchoolEvents

72

Agnieszka Roguska A00188976

3 Create View Showing Event Description Target Amount with School name ,County and Patron

id , address and Email Address.

Create View Eventss_IN_Schools As

Select E.Description, E.Target_Amount, S.Name, S.County, P.Patron_Id, P.Address,

P.Email_Add

From Event E, School S, Patron P

Where P.Patron_Id = S.Patron_Id

And E.School_Id = S.School_Id;

4. Create view showing the highest and lowest Target Amount . Call them Maximum Target

and Minimum Target . Also count the difference between them and it Range of Target. Group

by description

Create View Range_View_Target_Amount As

Select Description , Max(Target_Amount) As Maximum_Target

,Min(Target_Amount)As Minium_Target ,Max(Target_Amount)-Min(Target_Amount)

as Range_of_Target

From Event

Group BY Description;

Page 74: Agnieszka_Roguska_CharitySchoolEvents

73

Agnieszka Roguska A00188976

5. Create a View Showing the Event id , description , Actual Amount , Target Amount. Only

show the Events where Description contains letter “a” and Actual Amount lesser than 195500

with Target Amount higher than 1500. With Check option

Create View Event_Desc As

Select Event_Id,Description, Target_Amount,Actual_Amount

From Event

Where Description Like ‘%o%’

And Actual_Amount < 195500

And Target_Amount > 1500;

Select *

From Event_Desc ;

Page 75: Agnieszka_Roguska_CharitySchoolEvents

74

Agnieszka Roguska A00188976

Select Count(*)

From Event;

Select Count(*)

From Event_Desc;

Insert into Event_Desc Values(122, ‘Auction’, 9765, 11558);

Insert into Event_Desc Values(123, ‘Cakes sale’, 9755, 11858);

Insert into Event_Desc Values(124, ‘Auction’, 9765, 11558);

Insert into Event_Desc Values(125, ‘Cakes sale’, 9795, 11758);

Select Count(*)

From Event;

Select Count(*)

From Event_Desc;

Rollback;

Select Count(*)

From Event;

Page 76: Agnieszka_Roguska_CharitySchoolEvents

75

Agnieszka Roguska A00188976

Select Count(*)

From Event_Desc;

Page 77: Agnieszka_Roguska_CharitySchoolEvents

76

Agnieszka Roguska A00188976

6. Create a View Showing the Event id , description , Actual Amount , Target Amount. Only

show the Events where Description contains letter “a” and Actual Amount lesser than 195500

with Target Amount higher than 1500. With Check option

Create View Event_Desc As

Select Event_Id,Description, Target_Amount,Actual_Amount

From Event

Where Description Like ‘%o%’

And Actual_Amount < 195500

And Target_Amount > 1500

With Check Option;

Select *

From Event_Desc ;

Page 78: Agnieszka_Roguska_CharitySchoolEvents

77

Agnieszka Roguska A00188976

Select Count(*)

From Event;

Select Count(*)

From Event_Desc;

Insert into Event_Desc Values(122, ‘Auction’, 9765, 11558);

Insert into Event_Desc Values(123, ‘Auction’, 9755, 11858);

Insert into Event_Desc Values(124, ‘Auction’, 9765, 11558);

Insert into Event_Desc Values(125, ‘Auction’, 9795, 11758);

Select Count(*)

From Event;

Select Count(*)

From Event_Desc;

Page 79: Agnieszka_Roguska_CharitySchoolEvents

78

Agnieszka Roguska A00188976

Rollback;

Select Count(*)

From Event;

Select Count(*)

From Event_Desc;

Page 80: Agnieszka_Roguska_CharitySchoolEvents

79

Agnieszka Roguska A00188976

7. Create a view showing School Names, Counties with School Category

Secondary. With Check option

Create View School_Category AS

Select School_ID, Name, County, Category

From School

Where Category = ‘Secondary’

With Check Option;

Select *

From School_Category ;

Page 81: Agnieszka_Roguska_CharitySchoolEvents

80

Agnieszka Roguska A00188976

Insert into School_Category Values (301,‘St Joseph’, ‘Westmeath’,’Primary’);

Insert into School_Category Values (302, ‘St johns’, ‘Westmeath’, ‘Primary’);

Page 82: Agnieszka_Roguska_CharitySchoolEvents

81

Agnieszka Roguska A00188976

8 Using the above view, attempt to delete all schools from Kildare from the

School_Event View . Correct it in the event of a failure to delete Kildare

Schools

Select *

From School_Category ;

Delete

From School_Category

Where County = ‘Kildare’;

Select *

From School_Category ;

Rollback;

Select *

From School_Category ;

Page 83: Agnieszka_Roguska_CharitySchoolEvents

82

Agnieszka Roguska A00188976