SQL Foundations I Module 2

Use AND, OR and comparison operators in WHERE

Only editable by group admins

  • Last updated August 24, 2016 at 1:05 PM
  • Evidence visible to public
Four queries will utilize various predicates involving multiple AND and OR clauses (a minimum of three total), with a variety of comparison operators. Provide a link to your work.

All posted evidence

WHERE the file is under 100 bytes OR over 9000 bytes

jaco Almost 9 years ago

Uses "WHERE ... AND" to narrow results down to one file.

jaco Almost 9 years ago

Task 2

SELECT `Salary(Millions)` from Soccer_Players WHERE `Salary(Millions)` > 3 and `Salary(Millions)` < 10

SELECT `Net Worth(Millions)`, name from Soccer_Players WHERE `Net Worth(Millions)` > 100 and `Net Worth(Millions)` < 400

SELECT name, age from Soccer_Players WHERE `Age` > 25 and age < 30

SELECT name, age,`Net Worth(Millions)` from Soccer_Players WHERE `Age` < 27 and `Net Worth(Millions)` > 30 or `Net Worth(Millions)` < 35
franciscocruz11 About 9 years ago

This is for the database in Konagora, chapter 2. http://edu.konagora.com/SQLsandbox.php#

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
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%' /* <- EVIDENCE HERE */
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,
    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 /* <- EVIDENCE HERE */
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',
  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 /* <- EVIDENCE HERE */
GROUP BY
  secret
HAVING
  secret != HEX('billy') and stack = 2
ORDER BY
  secret DESC, users.name ASC
;


/* query 4 */
SELECT MIN(LENGTH(name)) 
FROM users 
WHERE name != 'Billyjim Cricketwhisker' and (user_id % 2 = 1 or name like '%o%'); /* <- EVIDENCE HERE */
kfowler About 9 years ago

Task 2

I have all of my queries listed here, grouped by task:
https://drive.google.com/open?id=0B5hBlRd1P7vEbXF6azltbXBHU0E
  1. select * From Orders where DateOrdered='2016-05-01 08:43:07' or DateOrdered='2016-05-01 09:02:43'
  2. select * From Products where CaseQuantity=2 or NumberInStock<5
  3. select * From Customers where CustomerID='CuSto1234' and LastName='Stove'
  4. select * From Customers where (FirstName='Franken' and LastName='Otter') or Email='Derivingfromstuff@aol.com'
slewis52 About 9 years ago