縮排
沒有被廣泛接受的標準。每個人都同意的是,將所有內容壓縮成一行是不好的:
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;
至少,將每個子句放入一個新行,如果它們變得太長則拆分行:
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;
有時,引入子句的 SQL 關鍵字之後的所有內容都縮排到同一列:
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;
(這也可以在正確對齊 SQL 關鍵字時完成。)
另一種常見的風格是將重要的關鍵字放在他們自己的行上:
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;
垂直對齊多個相似表示式可提高可讀性:
SELECT Model,
EmployeeID
FROM Cars
WHERE CustomerID = 42
AND Status = 'READY';
使用多行會使 SQL 命令更難嵌入到其他程式語言中。但是,許多語言都有多線字串的機制,例如 C#中的 @"..."
,Python 中的 """..."""
或 C++中的 R"(...)"
。