Stats 21 - Lecture 25 - SQL part 2
Overview
Miles Chen demonstrates SQL query techniques on a sample employee database, using SQLAlchemy to retrieve results and pandas DataFrames to inspect them. Examples cover string concatenation, salary classification with CASE, random ordering, NULL handling with COALESCE, and pattern matching with LIKE; Chen also outlines a final exam question pool of about 100 questions, with 40 to 50 selected for the exam.
Key takeaways
- SQLAlchemy can retrieve database query results for analysis in Python, while pandas DataFrames provide a convenient way to inspect those results.
- SQL uses || for string concatenation, allowing fields such as an employee name and job title to be combined into a labeled output column.
- ORDER BY RANDOM() LIMIT 5 randomizes the full candidate set before returning five rows, unlike shuffling only the first five records.
- IS NULL checks for missing values, not zero; COALESCE can replace NULL with zero but may erase the distinction between missing and genuinely zero values.
- SQL LIKE patterns use percent signs as wildcards, so %I% finds strings containing I and %ER finds strings ending in ER.
- Chen plans a final exam drawn from a pool of about 100 questions, with approximately 40 to 50 questions selected and no questions outside the published pool.
Chapters
- Chen plans to publish a large multiple-choice question pool to guide final exam study.
- He distinguishes querying an existing database from creating or managing a database server.
- SQLAlchemy retrieves query results for inspection in pandas DataFrames.
- UCLA students can access SQL learning resources through O'Reilly, including material on PostgreSQL setup.
- The sample database contains employee and department tables, with employee numbers, names, job titles, managers, salaries, commissions, and department numbers.
- The department table lists department numbers 10, 20, 30, and 40 alongside department names and locations.
- SQL's double vertical bar operator, ||, concatenates employee names, literal text, and job titles into a message column aliased as MSG.
- Filtering the employee table to accounting shows Clark, King, and Miller, and their manager numbers illustrate the reporting chain.
- A CASE expression assigns lower, middle, or higher wage labels based on salary conditions and returns the result as a status column.
- The example selects employee name, salary, and status; King is the only employee shown in the higher-wage category.
- ORDER BY RANDOM() followed by LIMIT 5 returns five rows from a randomized ordering of the employee table.
- Because ordering happens before limiting, the five results are not simply the table's first five rows shuffled.
- The condition commission IS NULL identifies missing commission values, not employees whose commission is zero.
- Turner's zero commission is distinct from NULL, so he does not appear in the missing-value results.
- COALESCE(commission, 0) substitutes zero for missing commissions, but that makes missing values indistinguishable from actual zero commissions without another field.
- LIKE uses percent signs as wildcards: %I% matches names containing I, while %ER matches job titles ending in ER; the example also filters to departments 10 and 20.
- Chen browses O'Reilly references including SQL Cookbook, Learning SQL, and SQL Queries for Mere Mortals, which covers aggregates such as SUM, AVG, MIN, and MAX.
- He notes that UCLA's O'Reilly access includes learning material on SQL, Python, AI, and machine learning.
- Chen estimates a final exam pool of about 100 multiple-choice questions, with roughly 40 to 50 drawn for the exam.
- He says every final exam question will appear in the released pool, which is intended to direct study toward important course concepts.
Transcript excerpt
0:03 All right. Well, uh thank you all for uh joining me and uh I hope you all had a good uh Thanksgiving um uh holiday. And um and I apologize this is labeled the wrong week. It should be week 10 Monday. Uh but yeah, happy Monday and um I uh yeah, stay safe out there. There we had some earthquakes. I guess a storm is coming. So, um hope you guys are all safe and doing well. Um I am still working on the final exam. What I'm thinking of doing is I'm going to release a pool of questions um for and then the final exam will consist of uh drawing uh questions from
0:52 that pool. It It's going to be a fairly large pool of multiple choice questions that cover um you know, a a range of the different topics and uh things that we covered in the class. Um but uh but yeah, that's what I'm thinking. Um I'm hoping to get all of that um up and published by um by by Wednesday so you can start studying uh for the final. Okay. Um with that said, uh let's take a look at um today's lecture. And we are just kind of continuing on um with uh more uh SQL stuff. And so um
1:44 uh these examples come from this textbook uh from O'Reilly and you can um read it. Just type in uh we're looking for University California, Los Angeles. Okay, we'll click sign in. And um takes you to your regular UCLA page. And then where's my Here we go. Two-factor authentication. Please recognize my finger. There we go. Um And yeah, you can um you can read
2:31 through it. There's um O'Reilly has uh quite a bit of stuff covering um SQL
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Miles Chen.