So I have a table called members and another table called group.The leader of the group is also a member
To retrieve members,who are not leaders I did the following
code:
SELECT first_name, last_name, rank
FROM members
EXCEPT ALL
SELECT first_name, last_name, rank
FROM members INNER JOIN groups ON mid=leader; --edited gid as mid
Doing this in MySQL gives me a syntax error.What should I use for EXCEPT ALL in MySQL?
SELECT first_name, last_name, rank
FROM members
LEFT OUTER JOIN groups ON gid=leader
WHERE leader is null
Not sure if leader or gid is in the groups table. The column that is in the groups table must have a null check in the where clause.
subquery may do, something like:
SELECT first_name, last_name, rank
FROM members
WHERE id NOT IN (
SELECT leader
FROM groups
WHERE leader = members.id
)
We need to know your table structure to help you further
You can try with this scenario.
SELECT
r.col1,r.col2
FROM tabel1 r
WHERE ROW ( r.col1,r.col2 ) NOT IN(
SELECT
col1,col2
FROM tabel2
);
Related
I need to select from an employee table the names of those who are supervisors and those who are not. Therefore, I need two separate columns to return, one for the supervisors' names and one for the employees'.
To do that, I have tried using where exists like so
select concat(first_name, middle_name, last_name) as supervisor_name, concat(first_name, middle_name, ulast_name) as employee_name
from employees
where exists (select employee_name from employees where employees.id = department.supervisor_id);
I have also tried creating a union between two select clauses, like so:
select concat (first_name, middle_name, last_name) as supervisor_name
from employees
where exists (select * from department where employees.id = department.supervisor_id)
union
select concat (first_name, middle_name, last_name) as employee_name
from femployees
where exists (select * from department where employees.id != department.supervisor_id);
Note that the department is another table in which I have the supervisor's id numbers.
I have searched if I could use some sort of check constraint as an alternative but couldn't find it.
I also tried applying select distinct , in an attempt to "divide" the values returned but couldn't make it work as well.
Have also tried using an alias, but it returns that first_namein field is ambiguous. Here is the script:
select concat (first_name, middle_name, last_name) as supervisor_name, concat (first_name, middle_name, last_name) as employee_name
from employees
join employees as supervisor_name on department.supervisor_id = employees.id
join employees as supervisor_id on department.supervisor_id != employees.id;
You have to join with the department table.
You need to use table aliases in the SELECT list to avoid ambiguity.
select concat (s.first_name, s.middle_name, s.last_name) as supervisor_name, concat (e.first_name, e.middle_name, e.last_name) as employee_name
from employees AS e
JOIN department as d ON e.department_id = d.id
join employees as s on d.supervisor_id = s.id
I have 3 tables called patients, customers and deliveries. Those tables are in the same database called db.
All the tables equally have id, first_name, last_name, gender and only deliveries table has their own data. (the other 2 tables are currently empty.)
Now, I want to select all of them in 1 query but mysql throws an error this:
SELECT first_name, last_name, gender FROM paitents, customers,
deliveries GROUP BY people LIMIT 0, 50000 Error Code: 1052. Column
'first_name' in field list is ambiguous 0.047 sec .
This is how I tried:
SELECT first_name, last_name, gender
FROM patients, customers, deliveries
GROUP BY people;
How do I select all of the tables even if some tables currently have no data?
All the tables equally have id, first_name, last_name, gender and only deliveries table has their own data. (the other 2 tables are currently empty.)
Now, I want to select all of them in 1 query
I suspect that you are looking for union all:
SELECT first_name, last_name, gender FROM patients
UNION ALL
SELECT first_name, last_name, gender FROM customers
UNION ALL
SELECT first_name, last_name, gender FROM deliveries
This will combine all records available in the 3 tables in the resultset. On the other hand, using an (implicit) cross join like you do would generate a cartesian product of the 3 tables, with 9 columns (3 * 3) in the resultset (that is, if you fix the ambiguity on column names that you currently have).
If you want to eliminate duplicates accross tables, you can use union instead of union all.
If you want to limit the number of records in the resultset, you can do this as follows:
(
SELECT first_name, last_name, gender FROM patients
UNION ALL
SELECT first_name, last_name, gender FROM customers
UNION ALL
SELECT first_name, last_name, gender FROM deliveries
)
ORDER BY id
LIMIT 5000
Note that, functionaly this does require an order by clause, otherwise the ordering of the results is undefined (I assumed id).
Suppose there is a table with columns as details of student's performance.
Student(name,subject,marks,verdict('pass'/'fail')).
I need to have another column in this table which will be - the sum of marks of the subjects in which a particular student fails.
In MySQL I could have written :
select *,
select (sum(marks) from Student where name=s.name and verdict='fail')
from Student s;
But it doesn't work in hive.
ERROR: Unsupported SubQuery Expression Invalid subquery. Subquery in SELECT could only be top-level expression.
What can be done ?
you could try using left join on subquery
select s.*, t.marks
from Student s
left join (
select (name ,sum(marks) marks
from Student
where verdict='fail'
group by name)
) t on t.name = s.name
eventually try add a proper index on table student column name
select
`name`,
`verdict`,
concat_ws('|', collect_set(`subject`)),
sum(`marks`)
from Student
group by name,verdict
Table 1: Student(name,enrollment_no,dept,college_id)
Table 2: Faculty(name,faculty_id,dept,college_id)
Desired o/p:
Department, No. of Student, No. of faculty in one table form
query i've been trying :
select
dept,
count(distinct student.dept) as total,
count(distinct faculty.dept) as total1
from student
join faculty
on student.college_id=faculty.college_id
group by dept;
query is not giving any output.
after your help
query is now
select * from (
(select count(*)as stu_count,student.department,student.college_id
from student group by department)T1
inner join
(select count(*)as fact_count,faculty_per.department,faculty_per.college_id
from faculty_per group by department)T2
on T1.college_id=T2.college_id);
Thank you.
select * from ((select count(*)as stu_count,t2.dept,t2.college_id from Student group by dept)T1
inner join
(select count(*)as fact_count,t3.dept,t3.college_id from Facultygroup by dept)T2
on T1.college_id=T2.college_id);
No of students and faculty can be extracted by performing inner join on two separate queries. Distinct is not required as we are doing group by
I have a relationship table such that it has
employeeID | storeID
What would be the query to find out which employees worked at more than one store?
SELECT employeeID WHERE ???
And possibly also list each different stores just once per employee...
Use group by and having, as in:
select employeeID, count(*) from table group by employeeID having count(distinct storeID) > 1
This will give you the employees working at more than one store. Use this as a subquery to list the stores for each such employee.
you can try -
select distinct employeeID,StoreID from table1
where storeID in
(
select storeID from table1 group by storeID having count(distinct employeeID) >1
)
cor storing count and showing store ID also in one query you can use following query..
select a.employeeID,a.storeID,b.cnt
from table1 a,
(select employeeID,count(*) cnt
from table1
group by employeeID
having count(distinct storeID) >1) b
where a.employeID=b.employeeid