SQL Foundations I Module 2

Use single-row functions in query

Only editable by group admins

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

All posted evidence

Uses SUBSTRING and LENGTH to seperate extensions from filenames. I think my character limit may be set a bit too low.

jaco Almost 9 years ago

Uses UPPER to capitalize the language for all files with a .py extension and uses LENGTH of filenames to sort.

jaco Almost 9 years ago

task 3

SELECT Upper(`Name`), Lower(`Team`) FROM `Soccer_Players`

SELECT Team, substring(Team, 2, 6) FROM Soccer_Players

SELECT Name, Upper(substring(Name, 1, 4)),Team, Lower(substring(Team, 2, 6))  FROM Soccer_Players
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

query links here!

see comments!

/* query 1 */
SELECT
    articles.title,
    upper(articles.body) as content, /*EVIDENCE HERE*/
    articles.article_id,    
    COUNT(ReplyJoin.comment_id) as replies_count
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,  /*EVIDENCE HERE*/
    GROUP_CONCAT(comments.body SEPARATOR ' + ') AS comment_summary,
    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)  /*EVIDENCE HERE*/ > 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
  users.name,
  comments.body as 'content',
  HEX(SUBSTRING_INDEX(comments.body, ' ', -1)) as 'secret', /*EVIDENCE HERE*/
  COUNT(users.name) as 'stack'
FROM
  comments
LEFT OUTER JOIN 
  users
ON 
  users.user_id = comments.user_id
LEFT OUTER JOIN 
  comments as parent_comment
ON 
  comments.parent_id = parent_comment.comment_id
WHERE
  ((comments.parent_id % 2) = 0 and users.name != 'jables') or comments.parent_id = 5
GROUP BY
  secret
HAVING
  secret != HEX('billy') and stack = 2 /*EVIDENCE HERE*/
ORDER BY
  secret DESC, users.name ASC
;

/* query 4 */
SELECT 
(
  SELECT
    concat_ws(' - ', articles.article_id, articles.title, COUNT(comments.comment_id)) /*EVIDENCE HERE*/
  FROM
    articles
  LEFT OUTER JOIN
    comments
  ON
    comments.article_id = articles.article_id
  GROUP BY
    articles.article_id
  ORDER BY
    COUNT(comments.comment_id) DESC
  LIMIT 1
) 
AS 'article with most replies (id - title - count)',
(
  SELECT
    concat_ws(' - ', users.name, COUNT(comments.comment_id)) /*EVIDENCE HERE*/
  FROM
    users
  LEFT OUTER JOIN
    comments
  ON
    users.user_id = comments.user_id
  GROUP BY
    users.user_id
  ORDER BY
    COUNT(comments.comment_id) DESC
  LIMIT 1
) 
AS 'user with most comments (name - count)',
(
  SELECT
    CONCAT_WS(' - ', article_id, MAX(LENGTH(body))) /*EVIDENCE HERE*/
  FROM
    articles
) 
AS 'article with most chars (id - count)',
(
  SELECT
    CONCAT_WS(' - ', comment_id, MAX(LENGTH(body))) /*EVIDENCE HERE*/
  FROM
    comments
) 
AS 'comment with most chars (id - count)';
;
kfowler About 9 years ago

Task 3

I have all of my queries listed here, grouped by task:
https://drive.google.com/open?id=0B5hBlRd1P7vEbXF6azltbXBHU0E
  1. select upper(ItemDescription) from Products;
  2. select CustomerID, substring(CustomerID, 1, 5) from Customers;
  3. select lower(CustomerID), upper(substring(FirstName, 1,2)), upper(substring(LastName, 1,3)) from Customers;
slewis52 About 9 years ago