Join Bill Weinman for an in-depth discussion in this video Understanding joins, part of SQL Essential Training (2014).
It's the nature of a relational database that some tables contain information related to other tables. Using the join statement, SQL has powerful tools for extracting related data from multiple tables. Typically unique ID fields are used to create relationships. All modern database systems support automatic generation of these ID fields, and they work well for creating and managing simple or complex relationships between tables. When you need a result that includes related rows from multiple tables, you'll need to use a joined query.
It's easy to understand joins if you visualize your tables as a Venn diagram, where each of your tables are represented by intersecting shapes. The intersection of the shapes where the tables overlap, are the records where our condition is met. I.D. fields are often used for this purpose where the condition to be met is matching ID's. The simplest and most common form of a join is the inner join. This is the default and it's the join you get when you use the join key word by itself.
The result of an inner join will include rows from both tables where the joined condition is met. The inner key word is typically omitted as this is the default join. The outer join is less common, but still important to understand. A left outer join includes the rows where the condition is met, plus all the rows from the table on the left, where the condition is not met. The outer key word is typically omitted as a left join is presumed to be an outer join. Likewise, a right outer join includes all the rows from the table on the right.
This is considered a special case in many databases don't support right joins. SQL Light, the database we're using for the demonstrations in this course, does not support right joins. Generally a right join can be written as a left join by simply changing the order of the tables in the query. A full outer join includes all the rows from both tables, including those where the condition is met. Many database systems including SQLite do not implement full outer join. There are many variations of these basic joins implemented in different ways by the different database system vendors.
For most purposes you'll just need to understand these basic concepts. For more specific implementation details, you should consult the documentation for your database system.
- Understanding SQL terminology and syntax
- Creating new tables and records
- Inserting and updating data
- Writing basic SQL queries
- Sorting and filtering
- Accessing related tables with JOIN
- Working with strings
- Finding the numeric type of a value
- Using aggregate functions and transactions
- Updating a table with triggers
- Creating views
Skill Level Beginner
Q: For Mac OS X: When I try to start the Apache Web Server from the XAMPP control panel, it doesn't start, and when I open "localhost" in my web browser, I see a white screen that says "It Works!" instead of the XAMPP page.
sudo apachectl stop
Q: I'm on a Mac, and I get an error in SID that says "attempt to write a read only database." How can I fix this?
A: This usually means that the database folder does not have sufficient permissions for writing by the web user. This can happen if you create the SQL folder new, rather than copying it from the Exercise Files. Here's how to fix this:
- Open a Finder window and Navigate to /Applications/XAMPP/htdocs/SQL
- Control-click on the SQL folder and select "Get Info" from the context menu.
- Under "Sharing and Permissions" (you may need to open the disclosure triangle), in the "everyone" row, select "Read & Write."Then you can close the Info window.
- Now repeat the process for the three *.db files inside the folder.