Download presentation
Presentation is loading. Please wait.
Published byElinor Horn Modified over 9 years ago
1
Writing Advanced SQL Queries BKF05 Liz Lucchese
2
Agenda SQL revealed What is it and why should I care? Advanced query interface So, how do I use this thing? Recipes for success Real world examples to learn syntax
3
SQL Revealed What is it? Structured Query Language A standardized way to query a database Why should I care? It’s used in advanced queries. It’s easy to learn. It’s fun!
4
Anatomy of a SELECT Statement SELECT * FROM STUDENT WHERE STD_YOG = 2007 ORDER BY STD_NAME_VIEW
5
Advanced Query Interface WHERE STD_YOG = 2007 SELECT * FROM STUDENT
6
Multiple Criteria WHERE STD_NAME_VIEW LIKE ‘A%’ AND STD_GRADE_LEVEL IN (‘01’,‘02’,‘03’) SELECT * FROM STUDENT
7
Parentheses WHERE (STD_YOG = 2007 AND STD_GRADE_LEVEL <> ‘12’) OR (STD_YOG = 2008 AND STD_GRADE_LEVEL <> ‘11’) OR (STD_YOG = 2009 AND STD_GRADE_LEVEL <> ‘10’)... SELECT * FROM STUDENT
8
Multiple Tables INNER JOIN PERSON ON PSN_OID = STD_PSN_OID WHERE PSN_DOB > ‘1999-12-31’ AND PSN_DOB <= ‘2000-12-31’ SELECT * FROM STUDENT
9
Nested Query (version 1) WHERE STD_OID NOT IN (SELECT CTJ_STD_OID FROM STUDENT_CONTACT) SELECT * FROM STUDENT
10
Nested Query (version 2) WHERE NOT EXISTS (SELECT CTJ_STD_OID FROM STUDENT_CONTACT WHERE STD_OID = CTJ_STD_OID) SELECT * FROM STUDENT
11
Multiple Nested Queries WHERE STD_OID IN (SELECT REQ_STD_OID FROM STUDENT_COURSE_REQUEST INNER JOIN COURSE_SCHOOL ON CSK_OID = REQ_CSK_OID INNER JOIN DISTRICT_SCHOOL_YEAR_CONTEXT ON CTX_OID = REQ_CTX_OID WHERE CSK_COURSE_NUMBER IN (‘101’,‘102’,‘103’) AND CTX_SCHOOL_YEAR = 2008) AND STD_OID NOT IN (SELECT REQ_STD_OID FROM STUDENT_COURSE_REQUEST INNER JOIN COURSE_SCHOOL ON CSK_OID = REQ_CSK_OID INNER JOIN DISTRICT_SCHOOL_YEAR_CONTEXT ON CTX_OID = REQ_CTX_OID WHERE CSK_COURSE_NUMBER = ‘202’ AND CTX_SCHOOL_YEAR = 2008) SELECT * FROM STUDENT
12
Aggregate Query WHERE STD_OID IN (SELECT ATT_STD_OID FROM STUDENT_ATTENDANCE WHERE ATT_DATE > ‘2006-09-01’ AND ATT_DATE < ‘2007-06-30’ AND ATT_ABSENT_IND = ‘1’ GROUP BY ATT_STD_OID HAVING COUNT(*) > 5) SELECT * FROM STUDENT
13
Extras W3Schools SQL Tutorial http://www.w3schools.com/sql/default.asp http://www.w3schools.com/sql/default.asp SQLzoo.net http://sqlzoo.net http://sqlzoo.net
14
Thank you. elucchese@x2dev.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.