Entity / Attributes Entity represents a person / thing Entity represents an Access table Attributes...

18
Entity / Attributes Entity represents a person / thing Entity represents an Access table Attributes describe facets of an entity Attributes represents columns in an Access table 1 CLIENT *ClientID ClientName ClientService s ClientCity ClientState ClientRevenue

Transcript of Entity / Attributes Entity represents a person / thing Entity represents an Access table Attributes...

Entity / Attributes• Entity represents a person / thing• Entity represents an Access table• Attributes describe facets of an entity• Attributes represents columns in an Access table

1

CLIENT

*ClientIDClientName

ClientServicesClientCity

ClientStateClientRevenue

Identifiers / Primary Keys• Every entity must be uniquely identified (via a primary key)• Asterisk represents the primary key• A primary key can be a single attribute or a combination of

attributes• Unique attributes can serve as the primary key (ClientName)• A primary key can be created (ID) in situations where there are

unique attributes

2

CLIENT

*ClientIDClientName

ClientServicesClientCity

ClientStateClientRevenue

Example: Client Table

3

Client

ClientIDClientName

ClientServices

ClientCity

ClientState

ClientRevenue

1 BK Associates Commercial Portland Oregon $210,000.00

2 Blaloc Industrial Kansas City Missouri $330,000.00

3 Bankton Electric Government New York New York $210,000.00

4 Bick Industrial Raleigh North Carolina $550,000.00

5 TX Electric Government Houston Texas $160,000.00

6 Crow Commercial Dallas Texas $270,000.00

7 GRB Industrial Atlanta Georgia $180,000.00

8 H&P Industrial Denver Colorado $90,000.00

9 LB&B Industrial Boston Massachusetts $211,000.00

10 Congro Industrial Atlanta Georgia $122,000.00

11 Moss Enterprises Commercial Phoenix Arizona $33,000.00

12 Ruby Industrial San Antonio Texas $344,000.00

13 Silver Industries Industrial Omaha Nebraska $218,000.00

14 TPH Commercial Annaheim California $166,000.00

Querying: Client TableTo show all of the data in the Client table.

SELECT * FROM Client;

4

ClientIDClientName

ClientServices

ClientCity

ClientState

ClientRevenue

1 BK Associates Commercial Portland Oregon $210,000.00

2 Blaloc Industrial Kansas City Missouri $330,000.00

3 Bankton Electric Government New York New York $210,000.00

4 Bick Industrial Raleigh North Carolina $550,000.00

5 TX Electric Government Houston Texas $160,000.00

6 Crow Commercial Dallas Texas $270,000.00

7 GRB Industrial Atlanta Georgia $180,000.00

8 H&P Industrial Denver Colorado $90,000.00

9 LB&B Industrial Boston Massachusetts $211,000.00

10 Congro Industrial Atlanta Georgia $122,000.00

11 Moss Enterprises Commercial Phoenix Arizona $33,000.00

12 Ruby Industrial San Antonio Texas $344,000.00

13 Silver Industries Industrial Omaha Nebraska $218,000.00

14 TPH Commercial Annaheim California $166,000.00

Selecting Specific Columns (vertical cut)

5

Client

ClientIDClientName

ClientServices

ClientCity

ClientState

ClientRevenue

1 BK Associates Commercial Portland Oregon $210,000.00

2 Blaloc Industrial Kansas City Missouri $330,000.00

3 Bankton Electric Government New York New York $210,000.00

4 Bick Industrial Raleigh North Carolina $550,000.00

5 TX Electric Government Houston Texas $160,000.00

6 Crow Commercial Dallas Texas $270,000.00

7 GRB Industrial Atlanta Georgia $180,000.00

8 H&P Industrial Denver Colorado $90,000.00

9 LB&B Industrial Boston Massachusetts $211,000.00

10 Congro Industrial Atlanta Georgia $122,000.00

11 Moss Enterprises Commercial Phoenix Arizona $33,000.00

12 Ruby Industrial San Antonio Texas $344,000.00

13 Silver Industries Industrial Omaha Nebraska $218,000.00

14 TPH Commercial Annaheim California $166,000.00

Selecting Specific Columns (vertical cut)Report a Client’s name, services, and revenues.

Display order based on query (i.e. in order of the comma separators)

SELECT ClientName, ClientServices, ClientRevenue FROM Client;

6

ClientName

ClientServices

ClientRevenue

BK Associates Commercial $210,000.00

Blaloc Industrial $330,000.00

Bankton Electric Government $210,000.00

Bick Industrial $550,000.00

TX Electric Government $160,000.00

Crow Commercial $270,000.00

GRB Industrial $180,000.00

H&P Industrial $90,000.00

LB&B Industrial $211,000.00

Congro Industrial $122,000.00

Moss Enterprises Commerical $33,000.00

Ruby Industrial $344,000.00

Silver Industries Industrial $218,000.00

TPH Commercial $166,000.00

Selecting Specific Rows (horizontal cut)

7

Client

ClientIDClientName

ClientServices

ClientCity

ClientState

ClientRevenue

1 BK Associates Commercial Portland Oregon $210,000.00

2 Blaloc Industrial Kansas City Missouri $330,000.00

3 Bankton Electric Government New York New York $210,000.00

4 Bick Industrial Raleigh North Carolina $550,000.00

5 TX Electric Government Houston Texas $160,000.00

6 Crow Commercial Dallas Texas $270,000.00

7 GRB Industrial Atlanta Georgia $180,000.00

8 H&P Industrial Denver Colorado $90,000.00

9 LB&B Industrial Boston Massachusetts $211,000.00

10 Congro Industrial Atlanta Georgia $122,000.00

11 Moss Enterprises Commercial Phoenix Arizona $33,000.00

12 Ruby Industrial San Antonio Texas $344,000.00

13 Silver Industries Industrial Omaha Nebraska $218,000.00

14 TPH Commercial Annaheim California $166,000.00

Selecting Specific Rows (horizontal cut)

Show all client data for client that contribute revenue less than $150,000.

SELECT * FROM Client WHERE ClientRevenue < 150000;

8

ClientIDClientName

ClientServices

ClientCity

ClientState

ClientRevenue

8 H&P Industrial Denver Colorado $90,000.00

10 Congro Industrial Atlanta Georgia $122,000.00

11 Moss Enterprises Commercial Phoenix Arizona $33,000.00

Selecting Specific Data Within Columns & Rows

9

Client

ClientIDClientName

ClientServices

ClientCity

ClientState

ClientRevenue

1 BK Associates Commercial Portland Oregon $210,000.00

2 Blaloc Industrial Kansas City Missouri $330,000.00

3 Bankton Electric Government New York New York $210,000.00

4 Bick Industrial Raleigh North Carolina $550,000.00

5 TX Electric Government Houston Texas $160,000.00

6 Crow Commercial Dallas Texas $270,000.00

7 GRB Industrial Atlanta Georgia $180,000.00

8 H&P Industrial Denver Colorado $90,000.00

9 LB&B Industrial Boston Massachusetts $211,000.00

10 Congro Industrial Atlanta Georgia $122,000.00

11 Moss Enterprises Commercial Phoenix Arizona $33,000.00

12 Ruby Industrial San Antonio Texas $344,000.00

13 Silver Industries Industrial Omaha Nebraska $218,000.00

14 TPH Commercial Annaheim California $166,000.00

Selecting Specific Columns & Rows

Display the Client’s name and revenues where revenue is greater than or equal to $270,000.

SELECT ClientName, ClientRevenue from Client Where ClientRevenue >= 270000;

10

ClientName ClientRevenue

Blaloc $330,000.00

Bick $550,000.00

Crow $270,000.00

Ruby $344,000.00

Selecting Specific Cases

List client id, name and state who are based in either Texas or Arizona.SELECT clientID, clientName, clientState FROM Client where clientState = 'Texas' or ClientState = 'Arizona';

also

SELECT clientID, clientName, clientState FROM Client where clientState in ('Texas', 'Arizona');

11

clientID clientName clientState

5 TX Electric Texas

6 Crow Texas

11 Moss Enterprises Arizona

12 Ruby Texas

Excluding Specific Cases

List client services and name for clients that provide services that are not Industrial.SELECT ClientServices, ClientName from Client where ClientServices <> 'Industrial';

also

SELECT ClientServices, ClientName from Client where ClientServices Not In ('Industrial');

12

ClientServices ClientName

Commercial BK Associates

Government Bankton Electric

Government TX Electric

Commercial Crow

Commercial Moss Enterprises

Commercial TPH

Displaying Output in Order - Columns

SELECT Clientname, ClientState from Client where ClientState = 'North Carolina';

SELECT ClientState, Clientname from Client where ClientState = 'North Carolina';

13

Client Name ClientState

Bick North Carolina

ClientState Clientname

North Carolina Bick

Displaying Output in Order - Rows

Display ClientName, ClientCity, ClientRevenue firms where revenues are greater than $180,000 in order of descending revenue. Where revenues are equivalent, list the Client firms in alphabetical order (based on their city).

SELECT ClientName, ClientCity, ClientRevenue FROM Client where ClientRevenue > 180000 order by ClientRevenue desc, ClientCity;

14

ClientName ClientCity ClientRevenue

Bick Raleigh $550,000.00

Ruby San Antonio $344,000.00

Blaloc Kansas City $330,000.00

Crow Dallas $270,000.00

Silver Industries Omaha $218,000.00

LB&B Boston $211,000.00

Bankton Electric New York $210,000.00

BK Associates Portland $210,000.00

SQL Functions and Calculations• COUNT, AVG, SUM, MIN, and MAX

Find the average Client RevenueSELECT Avg(ClientRevenue) as AverageRevenue FROM Client;

Display the Client name, state, and revenue (in thousands) for Client from TexasSELECT ClientName, ClientState, ClientRevenue/1000 as ThousandDollars FROM Client Where ClientState = 'Texas';

15

AverageRevenue

$221,000.00

ClientName ClientState ThousandDollars

TX Electric Texas 160

Crow Texas 270

Ruby Texas 344

Subqueries

• Subquery = A query within a query

Report all Client names and revenue for those firms with revenues that exceed the average revenue for the group as a whole.

SELECT ClientName, ClientRevenue From Client Where ClientRevenue > (SELECT AVG(ClientRevenue) From Client);

16

ClientName ClientRevenue

Blaloc $330,000.00

Bick $550,000.00

Crow $270,000.00

Ruby $344,000.00

Like / Not Like

List all Clients with a name starting with ‘B’.SELECT Clientname FROM Client Where ClientName Like 'B*';

List all Clients have do not have either an ‘S” or ‘B’ in their nameSELECT Clientname FROM Client Where ClientName Not Like '*B*' and

ClientName Not Like '*S*';

17

Clientname

BK Associates

Blaloc

Bankton Electric

Bick

Clientname

TX Electric

Crow

H&P

Congro

TPH

DISTINCTFind the number of different states represented by the Client base

SELECT Distinct ClientState as 'Client Locations' FROM Client;

18

'Client Locations'

Arizona

California

Colorado

Georgia

Massachusetts

Missouri

Nebraska

New York

North Carolina

Oregon

Texas