SQL Foundations I Module 2

rearrange columns and use aliases

Only editable by group admins

  • Last updated August 24, 2016 at 1:05 PM
  • Evidence visible to public
Two queries will demonstrate rearranging columns and assigning column aliases. Provide a link to your work.

All posted evidence

Rearranged and used aliases It all passed!

SELECT                 term_id as Semester,    student_id as Student,     grade_varchar as Performance FROM grade SELECT student_id as HUID,     student_name as Name,     dob as 'Date of Birth' FROM student


https://parkland1-my.sharepoint.com/:w:/r/personal/sdow3_stu_parkland_edu/_layouts/15/Doc.aspx?sourcedoc=%7B49198A6F-18AB-40C0-BF1C-94B3CD5DF86B%7D&file=Module%202_%20CSC176%20Data%20Systems_Sharon%20Dow%20102318.docx&action=default&mobileredirect=true

sdow3 About 7 years ago

Query 2

mpurnell1 About 8 years ago

Query 1

mpurnell1 About 8 years ago

Populating my tables so I have more to sort

INSERT INTO University (University_ID, University_Name) VALUES
  (2467548, "University of Illinois Urbana Champaign"),
  (5873461, "Illinois State University"),
  (3236472, "University of Chicago");
INSERT INTO Course (Professor, Course_No, Section, Credit_Hours, Course_Name, University_ID) VALUES
  ("J.W. Morrisette", 101, 003, 3, "Intro to Theatre", 1492059),
  ("Ryan Nerd", 451, 002, 2, "Advanced Gaming", 2467548),
  ("Jesus Christ", 131, 007, 4, "Transubstantiation", 1492059),
  ("Bruce Flachsbart", 270, 003, 3, "Gears", 2467548),
  ("Englie Ish", 105, 004, 2, "Words and Letters", 3236472),
  ("Johann Bach", 250, 002, 3, "Composition and Music Theory", 3236472),
  ("Michael Jordan", 325, 005, 1, "Slam Dunk", 2467548),
  ("I. M. Talking", 83, 001, 0, "History of Gossip", 5873461);
INSERT INTO Student (Username, ID, Major, University_ID, Course_No) VALUES
  ("easanga1", 6329864, "Costume Design", 1492059, 101),
  ("jpanell2", 5437269, "Mechanical Engineering", 2467548, 270),
  ("snagesh43", 3478295, "Computer Science", 2467548, 451),
  ("mbiehl7", 3738298, "English", 3236472, 105),
  ("brucey98", 1294874, "Music", 3236472, 250),
  ("sincz55", 7234829, "Recreation and Sport Tourism", 2467548, 325),
  ("jamen1", 3740144, "Religion", 1492059, 131),
  ("jbirch69", 1337420, "Communications", 5873461, 083);
mpurnell1 About 8 years ago

Not evidence, just included these statements as they are required to bring my tables from where they were in Module 1 to current setup.

/*Need to add Scrim back into Materials*/
INSERT INTO Materials VALUES(3, "Scrim", 0.76);
/*Need to add None as an option in Finishes table*/
INSERT INTO Finishes VALUES(4, "None", 0.00); 

/*need to populate values in Signs table*/
INSERT INTO Signs VALUES (1,2,3,1,3,2,12.00);
INSERT INTO Signs VALUES (2,3,2,2,2,1,5.72);
INSERT INTO Signs VALUES (3,6,3,1,1,1,10.80);
INSERT INTO Signs VALUES (4,9,3,2,2,2,38.74);
INSERT INTO Signs VALUES (7,4,4,5,4,4,98.56);
INSERT INTO Signs VALUES (8,6,4,5,4,1,36.96);
INSERT INTO Signs VALUES (9,3.25,4,5,4,1,20.02);
INSERT INTO Signs VALUES (10,3,4,5,4,1,18.48);
INSERT INTO Signs VALUES (11,3,4.5,5,4,1,20.79);
INSERT INTO Signs VALUES (12,2.25,3.25,4,4,1,5.56);
INSERT INTO Signs VALUES (13,4.5,3,1,1,1,8.46);
INSERT INTO Signs VALUES (14,9,3,1,2,1,15.86);
INSERT INTO Signs VALUES (15,9,3,2,2,2,38.74);
INSERT INTO Signs VALUES (16,1.33,2,1,4,2,2.77);
INSERT INTO Signs VALUES (17,3,1,1,4,1,1.56);
INSERT INTO Signs VALUES (18,2,3,2,4,1,3.90);
INSERT INTO Signs VALUES (19,3,2,1,4,1,3.12);
jmfisher About 8 years ago

image of results from sql statements

jmfisher About 8 years ago

SQL statements for rearranging columns and assigning aliases

/* Concatenated name and cost with reader friendly language and used alias of Available Materials for that column*/
SELECT concat( name, ' costs ', cost, ' per square foot.') AS "Available Materials" FROM Materials ORDER BY cost;

/* Displayed only certain columns, reordered them, concatenated vertical and horizontal dimensions into a more readable format with aliases*/
SELECT quantity AS "Quantity", concat(hor_dimens, "\' x ", vert_dimens, "\'") AS "Dimensions", cost AS "Cost" FROM Signs;
jmfisher About 8 years ago

Alias used on filename to specify the column would be only python programs.

jaco Almost 9 years ago

Simple query returning only shell scripts.

jaco Almost 9 years ago

Rearranging the order we return the values from people. Making an alias of title, called old_title. Old_title and year_released are columns.

SELECT peopleid, surname, first_name, born, died 
FROM people;

SELECT title AS old_title, year_released 
FROM movies 
WHERE year_released < 1990;
vbecu1 Almost 9 years ago

Hopefully this fixes past problems

Pastebin

Two queries will demonstrate rearranging columns and assigning column aliases - Pastebin.com

Two queries will demonstrate rearranging columns and assigning column aliases
randyiv Almost 9 years ago

see comments!

/* query 1 */

SELECT /* column order is rearranged */
    articles.title, 
    upper(articles.body) as content, /* alias */
    articles.article_id,    
    COUNT(ReplyJoin.comment_id) as replies_count /* alias */
FROM 
    articles
LEFT JOIN /*!! left join is used here because we want to get all articles with or without comments.*/
    comments AS ReplyJoin /* bonus alias */
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 /* column order is rearranged */
    lower(articles.title) as lower_title,  /* alias */ 
    GROUP_CONCAT(comments.body SEPARATOR ' + ') AS comment_summary, /* alias */
    articles.article_id
FROM 
    articles
LEFT JOIN /*!! left join is used here because we want to get all articles with or without comments.*/
    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
;
kfowler About 9 years ago