Is your data dragging you down? Are your tables all tangled up? Well we've got the tools to teach you just how to wrangle your databases into submission. Using the latest research in neurobiology, cognitive science, and learning theory to craft a multi-sensory SQL learning experience, Head First SQL has a visually rich format designed for the way your brain works, not a text-heavy approach that puts you to sleep.

Maybe you've written some simple SQL queries to interact with databases. But now you want more, you want to really dig into those databases and work with your data. Head First SQL will show you the fundamentals of SQL and how to really take advantage of it. We'll take you on a journey through the language, from basic INSERT statements and SELECT queries to hardcore database manipulation with indices, joins, and transactions. We all know "Data is Power" - but we'll show you how to have "Power over your Data". Expect to have fun, expect to learn, and expect to be querying, normalizing, and joining your data like a pro by the time you're finished reading!

Chapter 1 Data and Tables: A place for everything

Defining your data

Look at your data in categories

What’s in a database?

Your database viewed through x-ray specs...

Databases contain connected data

Take command!

Setting the table: the CREATE TABLE statement

Creating a more complicated table

Look how easy it is to write SQL

Create the my_contacts table, finally

Your table is ready

Take a meeting with some data types

Your table, DESCribed

You can’t recreate an existing table or database!

Out with the old table, in with the new

To add data to your table, you’ll use the INSERT statement

Create the INSERT statement

Variations on an INSERT statement

Columns without values

Peek at your table with the SELECT statement

Controlling your inner NULL

NOT NULL appears in DESC

Fill in the blanks with DEFAULT

Your SQL Toolbox

Chapter 2 The SELECT Statement: Gifted data retrieval

Date or no date?

Making contact

A better SELECT

What the * is that?

How to query your data types

More punctuation problems

Unmatched single quotes

Single quotes are special characters

INSERT data with single quotes in it

SELECT specific data

The old way

SELECT specific columns to limit results

SELECT specific columns for faster results

Doughnut ask what your table can do for you...

Ask what you can do for your doughnut

Combining your queries

Finding numeric values

Once is enough

Smooth Comparison Operators

Finding numeric data with Comparison Operators

Text data roping with Comparison Operators

Selecting your ingredients

To be OR not to be

The difference between AND and OR

Use IS NULL to find NULLs

Meanwhile, back at Greg’s place...

Saving time with a single keyword: LIKE

The call of the Wild(card)

That’s more LIKE it

Just BETWEEN us... there’s a better way

After the dates, you are either IN...

... or you are NOT IN

More NOT

Your SQL Toolbox

Chapter 3 DELETE and UPDATE: A change will do you good

Clowns are scary

Clown tracking

How our clown data gets entered

Bonzo, we’ve got a problem

Getting rid of a record with DELETE

Using our new DELETE statement

DELETE rules

The INSERT-DELETE two step

Be careful with your DELETE

The trouble with imprecise DELETE

Change your data with UPDATE

UPDATE rules

UPDATE is the new INSERT-DELETE

UPDATE in action

UPDATE your prices

All we need is one UPDATE

Your SQL Toolbox

Chapter 4 Smart Table Design: Why be normal?

Two fishy tables

A table is all about relationships

Atomic data

Atomic data and your tables

Reasons to be normal

The benefits of normal tables

Clowns aren’t normal

Halfway to 1NF

PRIMARY KEY rules

Getting to NORMAL

Fixing Greg’s table

The CREATE TABLE we wrote

Show me the

Time-saving command

The CREATE TABLE with a PRIMARY KEY

1, 2, 3... auto incrementally

Adding a PRIMARY KEY to an existing table

ALTER TABLE and add a PRIMARY KEY

Your SQL Toolbox

Chapter 5 Alter: Rewriting the Past

We need to make some changes

Table altering

Extreme table makeover

Renaming the table

We need to make some plans

Retooling our columns

Structural changes

ALTER and CHANGE

Change two columns with one SQL statement

Quick! DROP that column

A closer look at the non-atomic location column

Look for patterns

A few handy string functions

Use a current column to fill a new column

How our UPDATE and SET combo works

Your SQL Toolbox

Chapter 6 Advanced Select: Seeing your data with new eyes

Dataville Video is reorganizing

Problems with our current table

Matching up existing data

Populating the new column

UPDATE with a CASE expression

Looks like we have a problem

Tables can get messy

We need a way to organize the data we SELECT

Try a little ORDER BY

ORDER a single column

ORDER with two columns

ORDER with multiple columns

An orderly movie_table

Reverse the ORDER with DESC

The Girl Sprout® cookie sales leader problem

SUM can add them for us

SUM all of them at once with GROUP BY

AVG with GROUP BY

MIN and MAX

COUNT the days

SELECT DISTINCT values

LIMIT the number of results

LIMIT to just second place

Your SQL Toolbox

Chapter 7 Multi-Table Database Design: Outgrowing your table

Finding Nigel a date

Why change anything?

The query worked really well

It worked too well

Ignoring the problem isn’t the answer

Too many bad matches

Use only the first interest

A possible match

Mis-matched

Add more interest columns

Starting over

All is lost...

... But wait

Think outside of the single table

The multi-table clown tracking database

The clown_tracking database schema

An easier way to diagram your tables

How to go from one table to two

Linking your tables in a diagram

Connecting your tables

Foreign key facts

Constraining your foreign key

Why bother with foreign keys?

CREATE a table with a FOREIGN KEY

Relationships between tables

Patterns of data: one-to-one

Patterns of data: when to use one-to-one tables

Patterns of data: one-to-many

Patterns of data: getting to many-to-many

Patterns of data: we need a junction table

Patterns of data: many-to-many

Patterns of data: fixing gregs_list

Not in first normal form

Finally in 1NF

Composite keys use multiple columns

Even superheros can be dependent

Shorthand notations

Superhero dependencies

Partial functional dependency

Transitive functional dependency

Second normal form

We might be 2NF already...

Third normal form (at last)

And so, Regis (and gregs_list) lived happily ever after

The End

Your SQL Toolbox

Chapter 8 Joins and Multi-Table Operations: Can’t we all just get along?

Lynn Beighley

Lynn Beighley is a fiction writer stuck in a technical book writer's body. Upon discovering that technical book writing actually paid real money, she learned to accept and enjoy it.

After going back to school to get a Masters in Computer Science, she worked for the acronyms NRL and LANL. Then she discovered Flash, and wrote her first bestseller.

A victim of bad timing, she moved to Silicon Valley just before the great crash. She spent several years working for Yahoo! and writing other books and training courses. Finally giving in to her creative writing bent, she moved to the New York area to get an MFA in Creative Writing.

Her Head First-style thesis was delivered to a packed room of professors and fellow students. It was extremely well received, and she finished her degree, finished Head First SQL, and can't wait to begin her next book.

Lynn loves traveling, cooking, and making up elaborate background stories about complete strangers. She's a little scared of clowns.

Having worked in a support role for many years, I've been exposed to SQL while working with software developers but on a very basic level. Through the company, I took a 1 week introduction course to SQL and unfortunately, my duties lead me away from SQL and most of it was forgotten.

Years later, I decided to learn SQL again. I went to my local bookstore and went through the different SQL books and a lot of them just didn't explain the basics like why would you put a ';' at the end of a statement. I really wanted that ground level basic understanding of SQL and luckily I found Head First SQL!

What I really love about this book is how it clearly explains a line of code. There are a lot of exercises which makes you go over basic concepts over and over again. You really do get the concepts and feel comfortable about them. I feel confident that I understand SQL.

I'm still going through the book but so far it's been fun and rewarding. It's an excellent book and I highly recommend it.

I needed to refresh myself on SQL, and instead I not only did that but learned some new things too. But yeah, its all here. How to write queries and sub-queries, table normalization, etc. In a nice friendly style that really does stick with you as advertised.

I should probably mention up front that I am not really a member of the demographic that this book was written for (that being the fairly new students to relational database theory), but I liked this book nonetheless. What really endears the book to me, and in fact, the entire Head First series, is the geek humor that the author sprinkles liberally throughout the book. Wordplay, funny photo captions, and entertaining exercises abound to make sure that the difficult task of teaching something as dry as an introduction to SQL is as enjoyable and down-right entertaining as possible.

If you are familiar with other books in the Head First series, then you pretty much know what you are in for with this book. If you are not familiar with this series (and honestly, if not then why aren't you?) then you are in for somewhat of a different experience than your typical beginner level technical book. For starters, the book uses lots of visuals and graphics to explain things. If the topic is learning how a select statement works, then the author hits you with building a dating service to illustrate the points. If the difference between sub-selects and outer joins is the topic, then the author drags both out onto the metaphorical stage to have a debate over why you should use one or the other. All of these implements, from outlandish scenarios to anthropomorphic database constructs are cleverly woven together to make sure that the information the author is presenting sticks to your grey matter. If you do the many and varied exercises in each chapter, then you really can learn this stuff and have a fun time doing it.

The book doesn't assume that you have had any real experience with databases and it even has a chapter explaining why you would want to use a database in the first place. The content of the book also stays away from any database _specific information and sticks to generic SQL commonalities: selects, alter tables, updates, deletes, where-clauses, joins, sub-selects, aggregate functions, ordering, etc. Constraints, views, and some rudimentary security concerns are touched on, but not to any great degree.

The appendices are really a collection of esoteric topics that he author calls 'left-overs'. There is a quick section on PHP (which seems a little out of place in such a general SQL programming book), GUI tools for databases, a list of reserved words, some additional information on data types, etc. There is a larger section for how to download and install a MySQL database, which is as close as the book comes to endorsing one database vendor.

As with most books, there are a few minor things that bothered me about this book in particular, and about the Head First series as a whole. For starters, the pictures and glyphs that the book uses get somewhat redundant after the first few times that you see them. Chapter after chapter, you see the same actors in the same or slightly different poses. In many cases, the only difference seems to be the text in the dialog bubbles that are attached to the portraits. I joked with a colleague of mine that we should have a caption contest and write our own dialog for a good number of the examples in the book. This seems to be a systemic problem with the series itself rather than a problem with Head First SQL exclusively, because I own a number of the other books in the series and they also use many of the same graphics and photos and actors in those books. Perhaps, just perhaps, it is a fiendishly devised mnemonic device to see the same images time and time again, but it strikes me as a tad redundant and a little boring after seeing the same images used to explain different topics.

In the final analysis of the book, I have to recommend this book to anyone who may be just starting out on learning SQL programming. Don't expect this book to be the last book you will need to purchase on the subject if you are aiming to be a DBA, or even an enterprise developer, but it should definitely be the first one you buy.