SQL Foundations I Module 2

Use aggregate functions in query

Only editable by group admins

  • Last updated August 24, 2016 at 1:05 PM
  • Evidence visible to public
Four queries will utilize aggregate functions (each query must use a different function). Provide a link to your work.

All posted evidence

Uses COUNT to sort by number of duplicates.

jaco Almost 9 years ago

Uses SUM on namelength, filesize, and inodes cuz why not.

jaco Almost 9 years ago

Task 4 REVISED

Link to revised queries:
https://drive.google.com/open?id=0B5hBlRd1P7vEdk9zdzZRNVBpaDQ

select sum(ItemQty) from Orders;
SELECT CustomerID, COUNT(*) AS NumberOfOrders FROM Orders GROUP BY CustomerID ORDER BY CustomerID;
SELECT CustomerID, MAX(ItemQty) AS HighestQtyOrdered FROM Orders GROUP BY CustomerID ORDER BYCustomerID;
select min(DateOrdered) from Orders;
slewis52 Almost 9 years ago

Task 4

SELECT min(`Salary(Millions)`), max(`Salary(Millions)`) FROM `Soccer_Players`

SELECT max(age), min(age)  FROM `Soccer_Players`

SELECT sum(age), sum(`Net Worth(Millions)`) FROM `Soccer_Players`

SELECT age, Count(*) FROM `Soccer_Players` group by `Age`
franciscocruz11 About 9 years ago

This is for the database in Konagora, chapter 2.

Pastebin

CSC 176 Badge #2 - Pastebin.com

This is for the database in Konagora, chapter 2. I can't get SQLFiddle to work consistently enough and if I used my own DB I didn't think I'd be able to share it on badge for points.
randyiv About 9 years ago

link for query 4

kfowler About 9 years ago

link for query 3

kfowler About 9 years ago

link for query 2

kfowler About 9 years ago

link for query 1

kfowler About 9 years ago

see comments

/* query 1 */

SELECT
    articles.title,
    upper(articles.body) as content,
    articles.article_id,    
    COUNT(ReplyJoin.comment_id) as replies_count /*aggregate function*/
FROM 
    articles
LEFT OUTER JOIN 
    comments AS ReplyJoin
ON 
    ReplyJoin.article_id = articles.article_id
WHERE 
    (articles.body like '%SWEET%' and articles.user_id < 4) or articles.title like '%blandit%'
GROUP BY 
    articles.article_id
HAVING 
    COUNT(ReplyJoin.comment_id) > 1
ORDER BY 
    articles.title
;

/*query 2*/

SELECT
    lower(articles.title) as lower_title,   
    GROUP_CONCAT(comments.body SEPARATOR ' + ') AS comment_summary, /*aggregate function*/
    articles.article_id
FROM 
    articles
LEFT OUTER JOIN 
    comments
ON 
    comments.article_id = articles.article_id
WHERE
    articles.article_id < 5 and LENGTH(articles.body) > 50 and comments.parent_id is not NULL
GROUP BY 
    articles.article_id
HAVING
    NOT comment_summary like '%bart%'
ORDER BY
    LENGTH(articles.body) ASC
;

/*query 3*/
SELECT MIN(LENGTH(name)) /* aggregate func */
FROM users 
WHERE name != 'Billyjim Cricketwhisker' and (user_id % 2 = 1 or name like '%o%');

/*query 4*/
SELECT
    comment_id,
    MAX(LENGTH(body)) as 'length' /*aggregate function*/
FROM
    comments;
kfowler About 9 years ago

Task 4

I have all of my queries listed here, grouped by task:
https://drive.google.com/open?id=0B5hBlRd1P7vEbXF6azltbXBHU0E
  1. select sum(ItemQty) from Orders;
  2. select count(*) from Orders;
  3. select max(TotalCost) from Orders;
  4. select min(DateOrdered) from Orders;
slewis52 About 9 years ago