You are on page 1of 5

Using the provided Week 8 SQL file, create SQL statements to answer the following 5

questions. You will need to include the SQL statement and a screenshot.

1. What is the average weight of all the players in the database?

SELECT AVG(weight) FROM players;

2. Using COUNT, determine the number of players in the database based on their position.

SELECT position, COUNT(*) FROM players


GROUP BY position;

3. What is the birth date of the youngest player on each team? List the birth date and the team name.

SELECT MAX(dob), name


FROM teams t JOIN players p ON t.id = p.team_id
GROUP BY name;
4. Determine the average number of players on each team.

SELECT AVG(COUNT(id))
FROM players
GROUP BY team_id

5. Using the servicetablefee, determine the average discount that each department has offered for their services.  Be
sure to include the service name and display the amount with 2 decimals.

SELECT servicename, TO_CHAR(AVG(fee-feecharged), '99999.99')"Fee Discount"


FROM servicetablefee
GROUP BY servicename
Part 2:
For your homework assignment this week, you will create 4 SQL statements with group
function. For each query, list the question you are asking the database, your SQL
statement and a screenshot of your results. You may reuse queries from previous
assignments, but they will need to be recreated to include group function. Your queries
must include the following:

 1. COUNT

SELECT race, COUNT(*) Population FROM player


GROUP BY race;

 2. MIN or MAX

SELECT MIN(datecreated) FROM player;


SELECT MAX(datecreated) FROM player;

 3. AVG or SUM

SELECT TO_CHAR(AVG(GoldOwned), '9,999,999,999') "Average Gold for Knox" FROM player


WHERE SOUNDEX(playername) = SOUNDEX('Knox');

SELECT TO_CHAR(AVG(GoldOwned), '9,999,999,999') "Avg Gold in Database" FROM player;

 4. Nesting Example

SELECT TO_CHAR(AVG(SUM(GoldOwned)), '999999999')

FROM player

GROUP BY playername
 Also be sure to use GROUP BY, HAVING, ROLLUP, CUBE, GROUPING SETS

SELECT playername, profession_name, COUNT(*), AVG(GoldOwned)


FROM player p JOIN professions pr ON p.profession_id = pr.profession_id
GROUP BY CUBE (playername, profession_name)
ORDER BY playername, profession_name;

You might also like