Home
SQL tables should be joined with the JOIN keyword
Description
The rule "SQL tables should be joined with the JOIN keyword" states that when two or more tables are being combined in a SQL query, the JOIN keyword should be used to join the tables together. This ensures that the query is properly structured and that the data is properly retrieved from the database. The JOIN keyword allows for the use of various types of joins such as inner, left, right, and full outer joins. This allows the query to be more specific and to retrieve only the data that is needed. Additionally, the JOIN keyword allows for the use of various types of conditions such as equality, inequality, and range conditions. This allows for more flexibility when writing SQL queries.
Key Benefits
- Increased Efficiency: Joining tables with the JOIN keyword allows for more efficient data retrieval, as only the relevant data is returned.
- Simplified Queries: By using the JOIN keyword, complex queries can be simplified, making them easier to read and understand.
- Data Consistency: Joining tables with the JOIN keyword ensures that data is consistent across multiple tables, as the same data is used in each query.
Non-compliant Code Example
SELECT p.Name AS ProductName, pv.Name AS VendorName
FROM Production.Product p, Purchasing.ProductVendor pv; --Non compliant code (SQL JOIN conditions should involve all joined tables)
Compliant Code Example
SELECT p.Name AS ProductName, pv.Name AS VendorName
FROM Production.Product p
INNER JOIN Purchasing.ProductVendor pv --Compliant code (SQL JOIN conditions involving all joined tables)
ON pv.Product_Id = p.Id;