Skip to Content
Head First SQL
book

Head First SQL

by Lynn Beighley
August 2007
Beginner to intermediate
608 pages
13h 12m
English
O'Reilly Media, Inc.
Content preview from Head First SQL

Chapter 9. Subqueries: Queries within queries

image with no caption

Yes, Jack, I’d like a two-part question, please. Joins are great, but sometimes you need to ask your database more than one question. Or take the result of one query and use it as the input to another query. That’s where subqueries come in. They’ll help you avoid duplicate data, make your queries more dynamic, and even get you in to all those high-end concert afterparties. (Well, not really, but two out of three ain’t bad!)

Greg gets into the job recruiting business

So far, the gregs_list database has literally been a labor of love. It’s helped Greg find dates for his friends, but he’s made no money from it.

It occurs to him that he could start a recruiting business where he matches his contacts up with possible jobs.

image with no caption

Greg knows he’s going to need to add new tables for his contacts that are interested in the service. He decides to make them separate one-to-one tables rather than putting that information into my_contacts for two reasons.

First, not everyone in his my_contacts list is interested in the service. This way, he keeps NULL values out of my_contacts.

Second, he might hire people to help him with his business someday and the salary information might be considered sensitive. He may only want to give access to those tables to certain people ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Head First SQL, 2nd Edition

Head First SQL, 2nd Edition

Kimberly Fessel
Learning SQL

Learning SQL

Ben Forta
Practical SQL

Practical SQL

Anthony DeBarros

Publisher Resources

ISBN: 9780596526849Supplemental ContentErrata Page