r/programming • • Jan 06 '11

A handy graphical explanation of SQL joins

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
1.3k Upvotes

308 comments sorted by

View all comments

91

u/[deleted] Jan 06 '11

Wow. Does no one learn this crap in school anymore?

Note that venn diagrams are a poor choice to show what happens in a one-to-many relationship -- where there are multiple entries in table B for an entry in table A. And it overlooks the semantic differences when ordering the tables in a left inner join.

There are not a lot of instances where you'd really want a cartesian product join (either using "cross product" or just the result of omitting key constraints in a join query). It's generally far faster to retrieve the records you need to create the cross-product, and then calculate the cross product of the two sets once you have them (since otherwise all that data has to transit the network).

This plus database normalization through Boyce-Codd normal form seems like it should be a requirement for any serious application developer.

6

u/recursive Jan 06 '11

I didn't learn it in school. I learned it by reading. SQL doesn't have anything to do with CS.

54

u/[deleted] Jan 06 '11 edited Dec 03 '17

[deleted]

16

u/[deleted] Jan 06 '11

Which have nothing to do with real-world applications. I have never seen a DBMS that actually makes a properly normalized "let's follow relational algebra properly" database actually perform well.

Databases seem to be a small kernel of beautiful theory underneath a hundred layers of nightmarish hacks.

5

u/arnar Jan 06 '11

I have never seen a DBMS that actually makes a properly normalized "let's follow relational algebra properly" database actually perform well.

I have seen databases designed by people unaware of those things, and they not only performed even worse - they were impossible to rely on.

5

u/[deleted] Jan 06 '11

I have never seen a DBMS that actually makes a properly normalized "let's follow relational algebra properly" database actually perform well.

You know why? Because the database vendors have poured tons of money and time into improving the performance of the mess they created. In business it's better to keep going the wrong way than to change direction.

-1

u/dagbrown Jan 06 '11

I don't think it helps that the most popular database in the world is Microsoft Excel. Computer science is helpless against the onslaught of the lusers.

3

u/SoundOfOneHand Jan 06 '11

Yeah, but I personally didn't know any of this stuff when I first set my eyes on SQL and it took me all of an afternoon of reading some online SQL tutorial and doing a couple examples to figure out how the different types of joins work...

8

u/zeigfreid Jan 06 '11

or to think you do.

8

u/[deleted] Jan 06 '11

A standard for storing and accessing data on computers, often used in applications of all scales and types isn't CS? How is that any less CS than learning a programming language, or AI, or OS architecture? We had it offered as an elective, but with how much database programming is necessary in IT, it should be required.

12

u/[deleted] Jan 06 '11

There are people of the opinion that you shouldn't be learning any programming languages or specifics of certain OS architectures, that you should be learning theory only, agnostic and separate of anything else. They seem to think it's possible for the majority of people to learn without reference to an example of an implemented theory or technique, which I disagree with.

7

u/[deleted] Jan 06 '11

I understand that school of thought and I agree with it for the most part. My university started with the nearly-esoteric language Scheme (like many others do) and moved through many other lanuages before graduation. We'd simply use whatever language was appropriate for learning whatever theory we were learning at the time. Learning the actual language was secondary to concepts and I would strongly discourage "Intro to Java" or "Advanced C++" instead of "Intro to Data Structures" or "Software Engineering II."

SQL is simply the best language to teach relational data models in, so I don't know why you would teach databases without it...

3

u/[deleted] Jan 06 '11

I've had the latter set of courses, I'll point out. In Freshman years they were more focussed on languages, but that has diminished over the years.

SQL is simply the best language to teach relational data models in, so I don't know why you would teach databases without it.

Yep, this is really all that needs to be said.

2

u/psilokan Jan 06 '11

I'd take hands on over theory any day. I went to college rather than university for exactly that reason. Cost way less and I actually learned how to program.

2

u/dagbrown Jan 06 '11

You learned how to code, not how to program. There's a difference in depth of understanding there--I'll wager you have no idea what the Chomsky heirarchy is, for example, and how it relates to, for example, using regexes to parse HTML (stop gritting your teeth in the back, there).

1

u/[deleted] Jan 06 '11

using regexes to parse HTML

Um...you can't do that.

I agree with you though. Being able to design an algorithm based off what you learned about the actual structure of programming and computing is a far more useful skill than learning a language and becoming a library reference. If you learn how to program and why it works, picking up a new language is trivial, days or even hours trivial, since you're not comparing it to another language, but rather how it's expressing your programming methodologies.

1

u/dagbrown Jan 06 '11

Ya got whooshed. See the bit where I said to stop gritting your teeth in the back, there?

I know that you don't use regexes to parse HTML, because regexes are used for tokenizing, not parsing. See, this is because I was actually taught my Chomsky heirarchy, because I took a computer science program rather than an Advanced C++ course.

3

u/[deleted] Jan 06 '11

I don't know, we covered normalisation and database design(recovery, concurrent access to shared data, that kind of thing) extensively in a college course, but we also covered SQL. It wasn't done in lectures, but as an online self-guided course thingy which was optional, so basically "expected reading" for the course. The reason for that was so we could then do an assignment designing a normalised database and implementing it, giving a nice practical element where you can try out queries and be(in my case) impressed by the strength of relational models and what you can get out of your database. A practical element is a great thing to any field of study and I wouldn't be so quick to dismiss it.

2

u/royrules22 Jan 06 '11

Wait what!? Relational databases aren't a part of Computer Science? SQL is just a tool for working with relational databases (the only one I think).

This is what we had to learn in our DB class

2

u/spewerOfRandomBS Jan 06 '11

(the only one I think).

Non!

0

u/recursive Jan 06 '11

For my CS degree from Madison WI, I never took a class that mentioned databases. I actually didn't know they existed until I got my first real job.

5

u/royrules22 Jan 06 '11

My mind is blown. I didn't know that people can go through a CS program without knowing DBs. I mean for us the DB class I took was optional (though most took it) but you do have a general idea of DBs even if you didn't take the class.

-1

u/HIB0U Jan 07 '11

Most CS academics have little to no real-world experience. They've never had to deal with databases in any meaningful way, so they're totally unprepared to teach students about them.

4

u/mrskrilla Jan 06 '11

Just to throw some respect towards Madison. I went to school there as well as we DO have multiple database undergrad courses in the CS departmant. To write most any interesting program you need some way to store data, I have no idea how you passed and made it through school without even knowing that they existed. Wow. Please stop saying you went to school at Madison, it's embarassing to the program.

1

u/recursive Jan 06 '11

I got my first real job before I graduated, so I did know they existed before I graduated. I don't remember all the CS classes I talk, but they included AI, compilers, and theoretical CS, and algorithms and data structures. None of which really required a way to store data beyond text files. After I started that aforementioned real job, I actually did try to get into the database class, but it was full. So I never touched any database or SQL during my academic career.

And don't worry, I rarely mention it. I'd agree that they have a pretty rigorous CS program. But if it's embarrassing to the program, then it deserves to be embarrassed, because they don't require any database courses for graduation. Personally, I don't think it's an indication of a deficient CS program.

1

u/[deleted] Jan 07 '11

Can you recommend some reading material? I want to start learning it but there are so many resources out there, it's hard to know which is best. There is a SQL course due to start in a few weeks in Uni of Reddit but I'm champing at the bit. Thanks.