I have a table with this format
first_name last_name
and I want to show the pairs of "first_name" and "last_name" that appear exactly twice at the database. I've used this
select first_name, last_name, count(*)
from employees
having count(*)=2;
which is wrong and I am not sure what it's doing. It's my first sql approach so please be patient :) *Working with mysql workbench
You need a group by:
select first_name, last_name, count(*)
from employees
group by first_name, last_name
having count(*)=2;
select first_name, last_name, count(*)
from employees
group by first_name, last_name
having count(*)=2;
You have to use group by and then using having clause for this..
Query is:
select first_name, last_name, count() from employees group by first_name, last_name having count()=2;
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 an MYSQL database table of student info and their test scores per subject and I am trying to fetch each student's highest score in all subjects using the SQL query below
SELECT DISTINCT
first_name,
last_name,
subject_id,
(SELECT
MAX(score)
FROM
cbt_attempts_tbl
WHERE
first_name = first_name) AS MAX_SCORE
FROM
cbt_attempts_tbl
WHERE
score IS NOT NULL
ORDER BY first_name DESC
sample data
After running the query, the result I get are not as expected
In your subselect you need to link both tables, for example by using an alias
SELECT DISTINCT
first_name,
last_name,
subject_id,
(SELECT
MAX(score)
FROM
cbt_attempts_tbl
WHERE
first_name = f1.first_name) AS MAX_SCORE
FROM
cbt_attempts_tbl f1
WHERE
score IS NOT NULL
ORDER BY first_name DESC
But I don't really know what you are searching for, but following give the same, as DISTINCT doesn't make sense as long you don't have multiple rows with the same subject_id and name combination
SELECT
first_name,
last_name,
subject_id,
(SELECT
MAX(score)
FROM
cbt_attempts_tbl
WHERE
first_name = f1.first_name) AS MAX_SCORE
FROM
cbt_attempts_tbl f1
WHERE
score IS NOT NULL
GROUP BY first_name,
last_name,
subject_id
ORDER BY first_name DESC
If you are not worried about students without any score , then your query would be like
SELECT first_name, last_name, subject_id, MAX(score) FROM cbt_attempts_tbl GROUP BY first_name, last_name, subject_id ORDER BY first_name DESC
Otherwise, if you want to keep a full student list, you need to make a left joined query like
SELECT DISTINCT t.first_name, t.last_name, scores.subject_id, scores.subject_max_score FROM cbt_attempts_tbl t LEFT JOIN (SELECT first_name, last_name, subject_id, MAX(score) AS subject_max_score FROM cbt_attempts_tbl GROUP BY first_name, last_name, subject_id) scores ON scores.first_name = t.first_name AND scores.last_name = t.last_name GROUP BY t.first_name, t.last_name, scores.subject_id ORDER BY t.first_name DESC
At last, if to be more comprehensive, your database design is not correct. You should have a students table and your cbt_attempts_tbl table should have a foreign key student_id that references a students table id column. And then it would be completely different query - select from students left joined by cbt_attempts_tbl on foreign key condition
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).
I have the following query :
SELECT directory_auth_id, first_name, last_name, COUNT(user_info.directory_auth_id) as Duplication
FROM user_info
GROUP BY directory_auth_id, first_name, last_name
HAVING COUNT(*) > 1
ORDER BY directory_auth_id ASC
This gives me the desired results and shows me all records that meet the criteria.. What it does not do, is show me ALL the records.. How do I see all records that have been matched.
Thanks,
Boardman.
Assuming the SQL Server tag is correct, the best approach is to use window functions:
select ui.*
from (select ui.*, count(*) over (partition by directory_auth_id, first_name, last_name) as cnt
from user_info ui
) ui
where cnt > 1
order by cnt desc, directory_auth_id, first_name, last_name;
Unfortunately, MySQL does not support this ANSI standard functionality. But there are other approaches to solving the problem.
For SQL Server only...
To display all rows related only to the directory_auth_id that exist more than once, you have to determine which directory_auth_id have duplication, then use that resultset as a filter on the main table.
This will accomplish that.
;WITH DUPES
AS
(
SELECT directory_auth_id
FROM user_info
GROUP BY directory_auth_id
HAVING COUNT(*) > 1
)
SELECT directory_auth_id, first_name, last_name
FROM user_info T1
JOIN DUPES T2 ON T1.directory_auth_id = T2.directory_auth_id
This may work for you.
SELECT * FROM user_info where (directory_auth_id,first_name,last_name) in (
SELECT directory_auth_id, first_name, last_name
FROM user_info
GROUP BY directory_auth_id, first_name, last_name
HAVING COUNT(*) > 1
)
ORDER BY directory_auth_id ASC
Try the following, this puts your query into an inline view and then joins to it based on the three relevant fields. So you would get all records from user_info that have more than one line for a (directory_auth_id, first_name, last_name) combination.
select x.*
from user_info x
join (select directory_auth_id,
first_name,
last_name,
count(*) as duplication
from user_info
group by directory_auth_id, first_name, last_name
having count(*) > 1) y
on x.directory_auth_id = y.directory_auth_id
and x.first_name = y.first_name
and x.last_name = y.first_name
This is for MSSQL:
select
directory_auth_id,
first_name,
last_name,
case when count(user_info.directory_auth_id)
over (partition by directory_auth_id, first_name, last_name) > 1
then count(user_info.directory_auth_id)
over (partition by directory_auth_id, first_name, last_name)
end as Duplication
from user_info;
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
);