Why the query is not working? - sql

I have made the schema on the sqlfiddle.
I am beginner to database.
http://sqlfiddle.com/#!4/21535
I want to get all employees in the cleaning department
select * from emp,department
where dname = 'cleaning'
and dno = dnum;
select * from emp e,department d
where d.dname = 'cleaning'
and e.dno = d.dnum;
select * from emp as e,department as d
where d.dname = 'cleaning'
and e.dno = d.dnum;
But the third query is not working . why?
I am reading from the book Fundamentals_of_Database_Systems.(Elmasri)
There are many queries which has used as.
Am I wrong?

select * from emp e,department d
where d.dname = 'cleaning'
and e.dno = d.dnum;
works fine
Just get rif of as. It is used with columns
UPDATE with join
select * from emp e JOIN department d ON e.dno = d.dnum
where d.dname = 'cleaning';

Oracle doesn't support using statement AS to determine alias for tables. In SQL standart it can be used only for change name of columns in query result. However, most of DB systems support as also like creating aliase for table in query.

Related

How to use UPDATE JOIN correctly?

I tried to update a table row as below:
update EMPLOYEE set Salary=Salary * 1.15 where Dno=1;
which works fine, but I wanted to learn how to use UPDATE JOIN. so I use the Department Name instead of DNo.
I wrote below code:
update EMPLOYEE set Salary=e.Salary * 1.15 from EMPLOYEE e join DEPARTMENT d on e.DNo=d.DNumber where d.DName='Headquarters';
But the result for above code is that the SQL Command updates all the rows in EMPLOYEE table.
Where did I do wrong?
In BigQuery, change this to a filter in the where clause:
update EMPLOYEE e
set Salary = Salary * 1.15
where exists (select 1
from DEPARTMENT d
where d.DNumber = e.DNo and
d.DName = 'Headquarters'
);
The above is standard SQL and should work in any database. If you want a FROM clause, then use:
update EMPLOYEE e
set Salary = e.Salary * 1.15
from DEPARTMENT d
where d.DNumber = e.DNo and
d.DName = 'Headquarters';
There are (at least) two different ways of implementing FROM in an update, the "SQL Server method" and the "Postgres" method. In the SQL Server method, the table being updated should be in the FROM clause. In the "Postgres method", the table cannot be in the FROM clause. BigQuery adheres to the "Postgres method".
The above actually works in both methods, though.
Try to use table alias name in update
update Emp
Set Salary = Emp.Salary * 1.15
From EMPLOYEE As Emp
join DEPARTMENT As dep On Emp.DNo = dep.DNumber
Where dep.DName = 'Headquarters';

SQL Developer : Holding Multiple values in Variable From Query Result

I need help with query for sql developer.The requirement is a select statement which returns ID Column.I need to hold this ids in a variable and fetch the records associated with each id.Thanks for any help.
You can use join statement.
for example:
select id from table_1 join table_2
on table_1.id = table_2.id;
Not possible, as far as I can tell.
But, if it is the same query all over again and you use its result (those ID values you mentioned) in another queries, then you could create a view
create or replace view v_id as
select id
from some_table
where some_conditions;
and then use that view as yet another data source, e.g.
select d.dname, e.ename, e.job, e.sal
from emp e join v_id v on v.id = e.empno
join dept d on d.deptno = e.deptno;
or
select e.ename, e.job, e.sal
from emp e
where e.empno in (select v.id from v_id v);
or any other option you might choose.

How to use subqueries in oracle

Hello I'm trying to solve this question with a subquery:
Select names, service number, jobs and salaries of
people working in the same city as HAVET. (havet is a name)
And I have only two table the first one is the emp table with the column (noserv, name, job, salaries) and the second one is the SERV table with the column (noserv, nameserv, city)
I know that I have to use a subquery but I don't know how to do it.
Semi-pseudocode (CTE won't work, obviously).
with emp (noserv, name, job, salaries),
serv (noserv, nameserv, city)
-- This is what you're looking for, I presume
select e.*
from emp e join serv s on e.noserv = s.noserv
where s.city = -- subquery returns city where HAVET lives
(select s1.city
from serv s1 join emp e1 on e1.noserv = s1.noserv
where e1.name = 'HAVET'
);
Try this:
-- This is a normal query with a left join
select *
from emp e
left join s on e.noserv = s.noserv
where s.city =
-- get Havet's city from the subquery.
(select s.city
from emp e
left join s on e.noserv = s.noserv
where e.name = 'HAVET')
Try this one, change the column name according to your table and column names.
Select e.name,e.serviceNubmer,e.Job,e.salaries
from emp e,serv s
where
e.noserv = s.noserv
and s.city ='HAVET';

How do I simplify this query?

so my main goal with this query is to select all departments and employees under a manager named Mario Souza.
SELECT d.deptname,e.name FROM employee e JOIN department d ON e.dept = d.deptnum WHERE idmanager IN(
SELECT id FROM employee WHERE name = 'Mario Souza'
) AND manager IN(
SELECT id FROM employee WHERE name = 'Mario Souza'
)
And it's working, but is there a way I could store the first IN result so I could use it later after the AND operator?
You can use EXISTS to match on multiple columns.
WHERE EXISTS
(
SELECT *
FROM employee AS manager
WHERE manager.name = 'Mario Souza'
AND manager.id = e.idmanager
AND manager.id = d.manager
)
You could use a JOIN with employee table. Simply put both manager and idmanager in ON clause.

SQL query how to print out manager name?

I need to create a query to display employee name and number along with their super's name and super number. Listing should also include employees who don't have any supervisor.
Select e.ename,e.empno,super.ename,super from emp e;
I don't know how to print out the manager/supervisor name, i just need that.
Join the table to itself using an outer join:
select e.ename, e.empno, super.ename, super.empno
from emp e
left join emp super on super.empno = e.super_empno
The left join will still return rows from emp that do not have a super defined.
EDIT
Due to OP's comment, here's how to do it without a join (using a nasty correlated sub-query):
select
ename,
empno,
(select super.ename from emp where empno = e.super_empno) as super_name,
super_empno
from emp e;