While studying for the 1Z0-051 exam, I've read in many places that column aliases cannot be used in the WHERE clause.
However, when I submit the following query:
SELECT e.first_name, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id
AND e.first_name LIKE 'B%';
I get the following results:
FIRST_NAME DEPARTMENT_NAME
--------------------- -----------------------
Bruce IT
Britney Shipping
Can someone explain why these aliases are working? Even better, what's the overall rule/concept I'm missing?
You can not use column name aliases in a where clause, but table name aliases which you do.
Related
I am a beginner, but I am a little stumped as to why I keep getting errors. I thought specifying the table would work. Do I have to specify the database as well? If I have to add my project name 'April-17' also I will loose my mind.
SELECT departments.department_id
FROM april-17.employee_data.departments d
FULL JOIN april-17.employee_data.employees e
ON d.department_id=e.department_id
You probably want
SELECT d.department_id
FROM departments d
FULL JOIN employees e
ON d.department_id=e.department_id
If you only have SELECT department_id it won't know which table you want department_id from (could be d or could be e), hence the "ambiguous" error message. But you have aliased the table to d so you also can't use SELECT departments.department_id, hence the "unrecognized name" error message.
Do you mean your database name? What errors are you getting? I will re-write your query, I hope you find this useful
SELECT <insert list of field you want select here> FROM departments d JOIN employee_data e on d.department_id = e.department_id
I am new to this - The below query is showing an error - employees and departments are tables and departmnet_id is the column name.
SELECT
name,
department_id,
role
FROM
employee_data.employees
INNER JOIN
employee_data.departments
ON
'august-emitter-173906.employee_data.employees.department_id' = 'august-emitter-173906.employee_data.departments.department_id'
This is because your department_id in your select could refer to either department id from departments or employees.
Try using an alias like:
SELECT
emp.name, emp.department_id, emp.role
FROM employee_data.employees emp
INNER JOIN employee_data.departments dep
ON emp.department_id = dep.department_id
Note: not sure which one is the correct table to use in your case but simply swap the alias as needed.
Example error:
As you can see here, I am not using an alias, similar to your question, and there is an ambiguity error:
Example solution:
With alias the ambiguity error is resolved
I'm just starting out..experimenting with simple queries. In the JOIN query below, the backticks in the FROM clause prevent the query from running - error is "Unrecognized name: employees at [9:5]" Remove the backticks, however, and the query runs just fine. Why is this? Thank you!
SELECT
employees.name AS employee_name,
employees.role AS employee_role,
departments.name AS department_name
FROM
`employees_data.employees`
INNER JOIN
employees_data.departments ON
employees.department_id = departments.department_id
If you are using quoted identifiers, you must quote the database and table name separately, thusly:
SELECT
employees.name AS employee_name,
employees.role AS employee_role,
departments.name AS department_name
FROM
`employees_data`.`employees`
INNER JOIN
employees_data.departments ON
employees.department_id = departments.department_id
When you quote an identifier like employees_data.employees that means the whole identifier. from `employees_data.employees` quote the .. It means "from the table employees_data.employees". In contrast to from employees_data.employees or from `employees_data`.`employees` which means "from the table employees in the schema employees_data".
Quote the individual identifiers, employees_data and employees; the . is syntax.
In general, don't quote identifiers if you don't have to. It can introduce other odd behavior. For example, SQL is generally case-insensitive, but quoting identifiers will make it case-sensitive. employees will match employees or Employees or EMPLOYEES but `employees` only matches employees.
Also, use schema qualifiers consistently. In your query sometimes you do and sometimes you don't. For example, your inner join is confusing. You're joining employees_data.departments on employees and departments. Is departments the same as employees_data.departments? Is employees_data.employees the same as employees?
If they're all in the same schema, don't use schemas. Fully qualifying your table names unnecessarily artificially restricts your query to only work on that schema; your query will break if the schema name changes.
SELECT
employees.name AS employee_name,
employees.role AS employee_role,
departments.name AS department_name
FROM
employees
INNER JOIN
departments ON employees.department_id = departments.department_id
This will query tables in the current schema, regardless of its name.
Say I have a query:
select
Last_Name,
First_Name,
e.EmployeeID
from
employee e
join
Employee_his eh on e.EmployeeID = eh.EmployeeID
First name and Last name are part of the employee table while employeeId is part of both.
Does adding the table alias e in front of last_name, first_name alter the statement in any way? I thought not but am told it will give me the wrong answer. Sorry about the simple question, it's a hard thing to look up.
This is your query:
select Last_Name, First_Name, e.EmployeeID
from employee e join
Employee_his eh
on e.EmployeeID = eh.EmployeeID;
In this case, the qualified names are needed, because EmployeeId --
with no qualification -- would be ambiguous. Which table would the column be referring to?
When you have multiple tables in a query, you should qualify all column names, so it is clear what the query is doing -- for others reading the query and for you months later.
I should note that ANSI SQL has the using clause. In many databases, the query could be written as:
select Last_Name, First_Name, EmployeeID
from employee e join
Employee_his eh
using (EmployeeId);
However, SQL Server does not support the using clause.
If both tables have a field with the same name, then yes, you need to have the table name or an alias in front of the field name.
I'm a student entering the world of SQL for the first time and I cannot find any clear and understandable documentation on how to use to_somedatatype(null) to substitute for missing columns in a UNION, INTERSECT, etc. I'm using the HR schema that comes build in with Oracle Database for my first few projects and the following query works (I get a list of the employee_id numbers for only those employees with dependents):
SELECT employee_id "Employees with Dependents"
FROM employees
INTERSECT
SELECT relative_id
FROM dependents;
However, as soon as I want to include the names of the employees and dependent birth dates, I'm stuck. This variation perform an error-free search but yields no results (outputs "no rows selected"):
SELECT e.employee_id "Employees with Dependents",
e.first_name,
e.last_name,
to_date(NULL) "Dependent Birthdate"
FROM employees e
INTERSECT
SELECT d.relative_id,
TO_CHAR(NULL),
TO_CHAR(NULL),
d.birthdate
FROM dependents d;
Several variations replacing to_date with to_number or just plain null and to_char with to_varchar2 or null fail to produce any rows.
What I'd like to produce is a single row for each dependent which displays the guardian's employee number, first name, last name, and the birth date of the dependent. I'm confident that my problem is the use of these null placeholders, combined with limited understanding of set operators in general. (My understanding is that, when I used a null placeholder like to_date(null), it will not be included in the comparison since there's nothing there to be compared to.)
Can someone please explain to me how to properly use these placeholders?
You must match the data type (using the TO_CHAR function or
any other conversion functions) when columns do not exist in
one or the other table. for example:
SELECT location_id, department_name "Department",
TO_CHAR(NULL) "Warehouse location"
FROM departments
UNION
SELECT location_id, TO_CHAR(NULL) "Department",
state_province
FROM locations;
let you relate with your scenario
The reason your queries don't work is that you have no NULL values in the corresponding fields. You are using INTERSECT. Actually, you want to join the tables together, something like:
SELECT e.employee_id "Employees with Dependents",
e.first_name,
e.last_name,
d.birthdate "Dependent Birthdate"
FROM employees e join
dependents d
on d.relative_id = e.employee_id