Page 35 - SQL
P. 35
(This can also be done while aligning the SQL keywords right.)
Another common style is to put important keywords on their own lines:
SELECT
d.Name,
COUNT(*) AS Employees
FROM
Departments AS d
JOIN
Employees AS e
ON d.ID = e.DepartmentID
WHERE
d.Name != 'HR'
HAVING
COUNT(*) > 10
ORDER BY
COUNT(*) DESC;
Vertically aligning multiple similar expressions improves readability:
SELECT Model,
EmployeeID
FROM Cars
WHERE CustomerID = 42
AND Status = 'READY';
Using multiple lines makes it harder to embed SQL commands into other programming languages.
However, many languages have a mechanism for multi-line strings, e.g., @"..." in C#, """...""" in
Python, or R"(...)" in C++.
Joins
Explicit joins should always be used; implicit joins have several problems:
• The join condition is somewhere in the WHERE clause, mixed up with any other filter
conditions. This makes it harder to see which tables are joined, and how.
• Due to the above, there is a higher risk of mistakes, and it is more likely that they are found
later.
• In standard SQL, explicit joins are the only way to use outer joins:
SELECT d.Name,
e.Fname || e.LName AS EmpName
FROM Departments AS d
LEFT JOIN Employees AS e ON d.ID = e.DepartmentID;
• Explicit joins allow using the USING clause:
SELECT RecipeID,
https://riptutorial.com/ 17

