Download presentation
Presentation is loading. Please wait.
Published byLeslie McKenzie Modified over 6 years ago
1
Director of Statistical Analysis and Assessment Coordinator
IR 101: Beginning SQL in IR Kelly Wahl Director of Statistical Analysis and Assessment Coordinator UCLA
2
Learning Outcomes Know what SQL is Read a basic SQL SELECT statement
Find online opportunities to learn the language Recognize examples of SQL syntax and identify what they do Consider how the results from a SELECT statement can be formatted to meet your needs
3
What is SQL? Structured Query Language (SQL)
Often pronounced “sequel” – because the original acronym was SEQUEL (Structured English QUEry Language) Developed by IBM in the early 70’s Changed its name because another company had already trademarked SEQUEL
4
How And When Do We Use SQL?
Its name gives away its purpose: It is a programming language used for managing data and querying databases. SQL has been “structured” to be clear, to sequence commands, to allow options to be pursued, and to block (i.e., to treat groups of statements as one). Relational Database Management Systems (RDBMS) can use it. Oracle Database, SQL Server, and MySQL are examples of database software that use this language for data access and management.
5
How Can I Learn SQL on My Own?
Free Tutorials Online: Give Me Your Info Online (But Free):
6
Properties of Relational Databases
Relational databases are for storing and retrieving related information. They can be explained with diagrams showing the way the data are “modeled.” Records are stored as rows in tables that have columns for each variable being stored per case.
7
A Relational Database:
major_degree_table declared_major_table term_table Major | Degree student_table ID | Term | Major Term | Year ID | Name | Gender term_enrollment_table subj_dept_table ID | Term | Subject | Catlg_No | Grade Subject | Dept dept_org_table ID | Ethnicity Dept | Org student_ethnicity_table
8
On a SQL Console: A List of Tables Is Provided
Clicking around Reveals Columns and Properties
9
Commands Results
10
What Makes a SQL Statement Work?
Syntax columns SELECT name FROM student_table WHERE gender = ‘Male’ table filter
11
How to Select Records SELECT name FROM student_table
WHERE gender = ‘Male’ ID | Name | Gender
12
How to Select Records SELECT name FROM student_table
Robert Frank Juan Tongshan Denzel SELECT name FROM student_table WHERE gender = ‘Male’
13
More Complicated Selections
WILDCARD (give me all of them) SELECT * FROM term_enrollment_table WHERE (subject = ‘Math’ or subject = ‘Chem’) and term = ’17F’ and substring(catlg_no,2,1) <= ’1’ All Math enrollments and all Chemistry enrollments, in Fall 2017, at the undergraduate course level…
14
More Complicated Selections
FROM term_enrollment_table WHERE (subject = ‘Math’ or subject = ‘Chem’) and term = ’17F’ and substring(catlg_no,2,1) <= ’1’ ID | Term | Subject | Catlg_No | Grade 17F Math A 17F Chem B 17F Chem B+ 17F Chem 0055A C 17F Math A . . .
15
‘0032A’ ‘0205’ Returns: ‘0’ Returns: ‘2’ Selected? YES Selected? NO
substring(catlg_no,2,1) <= ’1’ ‘0032A’ ‘0205’ Returns: ‘0’ Returns: ‘2’ Selected? YES Selected? NO
16
Little Aggregated Reports
SELECT subject, COUNT(subject) enrollments FROM term_enrollment_table WHERE term = ’18S’ GROUP BY subject ORDER BY enrollments desc A list of the subjects offered in Spring 2018 with count of enrollments, in descending order…
17
Little Aggregated Reports
SELECT subject, COUNT(subject) enrollments FROM term_enrollment_table WHERE term = ’18S’ GROUP BY subject ORDER BY enrollments desc Subject | Enrollments Chem Math English 2011 Psych . . .
18
Coding New Columns SELECT
CASE WHEN subject in (‘Math’,’Chem’) THEN ‘STEM’ ELSE ‘Not STEM’ END STEM_flag, grade FROM term_enrollment_table WHERE term = ‘18F’ Grades for Fall 2018 marked as STEM or not STEM…
19
Coding New Columns SELECT
CASE WHEN subject in (‘Math’,’Chem’) THEN ‘STEM’ ELSE ‘Not STEM’ END STEM_flag, grade FROM term_enrollment_table WHERE term = ‘18F’ STEM_Flag | Grade STEM B STEM A- STEM C Not STEM A Not STEM B+ STEM D . . .
20
WHEN subject in (‘Math’,’Chem’) THEN ‘STEM’
CASE WHEN subject in (‘Math’,’Chem’) THEN ‘STEM’ WHEN subject in (‘English’,’Spanish’) THEN ‘HUMANITIES’ ELSE ‘Other’ END discipline_group The code will test until it finds a match, then return the ‘THEN’ value and go to the next row. The new column name appears after the ‘END’. Subject Discipline_Group Chem STEM Spanish HUMANITIES Math STEM Psych OTHER English HUMANITIES
21
Fancier Output for Reports
What if I want something that looks like this: Subject | Enrollment 16F | Enrollment 17F | Enrollment 18F Chem English Math Psych Spanish
22
Fancier Output for Reports
Or something like this: Term | Subject | A’s Awarded | B’s Awarded | C’s Awarded 17F Chem 17F English 17F Math 17F Psych 17F Spanish
23
Fancier Output for Reports
I’d have to create a new column …and aggregate at the same time. A CASE statement, plus a GROUP BY… ???
24
Term Trend of Total Course Enrollment per Subject
SELECT subject, SUM(CASE WHEN term = ‘16F’ then 1 else 0 end) enrollment_16F, SUM(CASE WHEN term = ‘17F’ then 1 else 0 end) enrollment_17F, SUM(CASE WHEN term = ‘18F’ then 1 else 0 end) enrollment_18F FROM term_enrollment_table GROUP BY subject ORDER BY subject Subject | Enrollment_16F | Enrollment_17F | Enrollment_18F Chem English Math Psych Spanish
25
Grade Counts per Subject in a Term
SELECT term, subject, SUM(CASE WHEN grade in (‘A+’,‘A’,’A-’) then 1 else 0 end) As, SUM(CASE WHEN grade in (‘B+’,‘B’,’B-’) then 1 else 0 end) Bs, SUM(CASE WHEN grade in (‘C+’,’C’,’C-’) then 1 else 0 end) Cs FROM term_enrollment_table WHERE term = ’17F’ GROUP BY term, subject ORDER BY term, subject Term | Subject | A’s | B’s | C’s 17F Chem 17F English 17F Math 17F Psych 17F Spanish
26
Learning Outcomes Know what SQL is Read a basic SQL SELECT statement
Find online opportunities to learn the language Recognize examples of SQL syntax and identify what they do Consider how the results from a SELECT statement can be formatted to meet your needs
27
Things You Need to Do with Data
?
28
Kelly Wahl UCLA kwahl@college.ucla.edu
Happy SQLing! Kelly Wahl UCLA
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.