JoinsTo combine data from two tables we use the SQL JOIN command, which comes after the FROM command. Database tables are used to organize and group data by common characteristics or principles. The JOIN command combined with ON is used to combine fields from separate tables. The JOIN command on its own will result in a cross product, where each row in the first table is paired with each row in the second table. Usually this is not what is desired when combining two tables with data that is related in some way. For that, we need to tell the computer which columns provide the link between the two tables using the word ON. What we want is to join the data with the same species id. SELECT * FROM surveys JOIN species ON surveys.species_id = species.species_id; ON is like WHERE. It filters things out according to a test condition. We use the table.colname format to tell the manager what column in which table we are referring to. The output of the JOIN command will have columns from the first table plus the columns from the second table. For the above command, the output will be a table that has the following column names:
Alternatively, we can use the word USING, as a short-hand. USING only works on columns which share the same name. In this case we are telling the manager that we want to combine surveys with species and that the common column is species_id. SELECT * FROM surveys JOIN species USING (species_id); The output will only have one species_id column
We often won’t want all of the fields from both tables, so anywhere we would have used a field name in a non-join query, we can use table.colname. For example, what if we wanted information on when individuals of each species were captured, but instead of their species ID we wanted their actual species names. SELECT surveys.year, surveys.month, surveys.day, species.genus, species.species FROM surveys JOIN species ON surveys.species_id = species.species_id;
Many databases, including SQLite, also support a join through the WHERE clause of a query. SELECT surveys.year, surveys.month, surveys.day, species.genus, species.species FROM surveys, species WHERE surveys.species_id = species.species_id; For the remainder of this lesson, we’ll stick with the explicit use of the JOIN keyword for joining tables in SQL.
Different join typesWe can count the number of records returned by our original join query. SELECT COUNT(*) FROM surveys JOIN species USING (species_id); Notice that this number is smaller than the number of records present in the survey data. SELECT COUNT(*) FROM surveys; This is because, by default, SQL only returns records where the joining value is present in the joined columns of both tables (i.e. it takes the intersection of the two join columns). This joining behaviour is known as an INNER JOIN. In fact the JOIN command is simply shorthand for INNER JOIN and the two terms can be used interchangably as they will produce the same result. We can also tell the computer that we wish to keep all the records in the first table by using the command LEFT OUTER JOIN, or LEFT JOIN for short.
Remember: In SQL a NULL value in one table can never be joined to a NULL value in a second table because NULL is not equal to anything, not even itself. Combining joins with sorting and aggregationJoins can be combined with sorting, filtering, and aggregation. So, if we wanted average mass of the individuals on each different type of treatment, we could do something like SELECT plots.plot_type, AVG(surveys.weight) FROM surveys JOIN plots ON surveys.plot_id = plots.plot_id GROUP BY plots.plot_type;
Functions COALESCE and NULLIF and moreSQL includes numerous functions for manipulating data. You’ve already seen some of these being used for aggregation (SUM and COUNT) but there are functions that operate on individual values as well. Probably the most important of these are COALESCE and NULLIF. COALESCE allows us to specify a value to use in place of NULL. We can represent unknown sexes with 'U' instead of NULL: SELECT species_id, sex, COALESCE(sex, 'U') FROM surveys; The lone “sex” column is only included in the query above to illustrate where COALESCE has changed values; this isn’t a usage requirement.
COALESCE can be particularly useful in JOIN. When joining the species and surveys tables earlier, some results were excluded because the species_id was NULL in the surveys table. We can use COALESCE to include them again, re-writing the NULL to a valid joining value: SELECT surveys.year, surveys.month, surveys.day, species.genus, species.species FROM surveys JOIN species ON COALESCE(surveys.species_id, 'AB') = species.species_id;
The inverse of COALESCE is NULLIF. This returns NULL if the first argument is equal to the second argument. If the two are not equal, the first argument is returned. This is useful for “nulling out” specific values. We can “null out” plot 7: SELECT species_id, plot_id, NULLIF(plot_id, 7) FROM surveys; Some more functions which are common to SQL databases are listed in the table below:
Finally, some useful functions which are particular to SQLite are listed in the table below:
As we saw before, aliases make things clearer, and are especially useful when joining tables. SELECT surv.year AS yr, surv.month AS mo, surv.day AS day, sp.genus AS gen, sp.species AS sp FROM surveys AS surv JOIN species AS sp ON surv.species_id = sp.species_id; To practice we have some optional challenges for you.
What type of join is used when you have to combine each row from the first table with each row of the second table?Cartesian Join also known as the Cross Join, returns the cartesian product of the tables being joined, which is the combination of every row of one table with every row of another table.
Which kind of join should you use if you want all the rows from both tables even if they don t match?FULL JOIN. A FULL JOIN or FULL OUTER JOIN is essentially a combination of LEFT JOIN and RIGHT JOIN . This type of join contains all of the rows from both of the tables.
Which of the following tells SQL to join data from two or more tables into a single result set?Joins are used to combine the rows from multiple tables using mutual columns.
Which of the following joins contains only rows from left table which match the joining condition without having any columns from the right table?Introduction to SQL Server LEFT JOIN clause
The LEFT JOIN clause allows you to query data from multiple tables. The LEFT JOIN returns all rows from the left table and the matching rows from the right table. If no matching rows are found in the right table, NULL are used.
|