The following is the SQL statement that I found:
SELECT d.name, count(e.emp_id) num_employees
FROM department d INNER JOIN employee e ON d.dept_id = e.dept_id
GROUP BY d.name
HAVING count(e.emp_id) > 2;I wanted to know what d.name, e.name or d.dept_id mean. What is the difference?
2 Answers
The dot . is usually the table name. In the sentence you mention, d is the name of the alias name. If you check the FROM statement, you have department d. That means that you want to fetch rows from the table department but reference it as d (because department is a long word), so you create an alias.
In order to disambiguate fields, as different tables may have the same field names, you can prepend the field with the table name (or alias in you case) followed by the dot.
So in short you're saying:
2Select the field
namefrom aliasd, and count the number of employees from the join of tablesdepartmentandemployee, aliaseddanderespectively, etc.
From Dev.MySQL.com:
You can refer to a table within the default database as
tbl_name, or asdb_name.tbl_nameto specify a database explicitly.
You can refer to a column ascol_name,tbl_name.col_name, ordb_name.tbl_name.col_name. You need not specify atbl_nameordb_name.tbl_nameprefix for a column reference unless the reference would be ambiguous.
MySQL permits names that consist of a single identifier or multiple identifiers. The components of a multiple-part name must be separated by period (“
.”) characters. The initial parts of a multiple-part name act as qualifiers that affect the context within which the final identifier is interpreted.
A table reference can be aliased using tbl_name AS alias_name or tbl_name alias_name:
SELECT t1.name, t2.salary FROM employee AS t1, info AS t2
WHERE t1.name = t2.name;
SELECT t1.name, t2.salary FROM employee t1, info t2
WHERE t1.name = t2.name;