How to format and print a result of sql query - mysql

I recently got this question on interview which I failed to answer. The question was to list the number of duplicates that appear in a column employer like from this table
id | employer | employee
1 | isro | dude1
2 | isro | dude 2
3 | cnd | dude 3
4 | df | dsfdef
5 | dfdf | dfdfd
...
so the result should be like
isro = 2
df = 4
dfsf = 6
how do i achieve this?
I know there is count(*) which i could use with select statement with where clause, but how do i achieve the above result.

The HAVING clause can be used to filter on aggregated values:
SELECT employer, COUNT(*)
FROM yourTable
GROUP BY employer
HAVING COUNT(*) > 1

assuming TableName is the name of the table you want to select from, this would be your answer.
SELECT employer, count(employer)
FROM TableName
GROUP BY employer
HAVING COUNT(*) > 1
here is an answer to a very similar question that has some more info for you.
How to count occurrences of a column value efficiently in SQL?

Related

Return users Min and Max values in single row,

I have a table full of user answers to a question.
I want to be able to construct a SQL statement which returns their first answer (min), and their most recent answer (max), ending up with a result like this:
user_id | first_answer | last_answer
1 | 50 | 100
2 | 10 | 5
...the table looks like this:
answer_id | user_id | answer
1 | 1 | 50
2 | 2 | 10
3 | 1 | 100
4 | 2 | 5
Sorry I don't have any code to show, but I genuinely have zero idea how to achieve this, so any help would be greatly appreciated.
Edit:
By min and max, I mean the first answer, and the most recent answer for each user.
You can build a query for that in two steps. First you get first and last answers for each user
select user_id, min(answer_id) min_answer, max(answer_id) max_answer
from yourTable
group by user_id
Then you join that with the original table twice to get the corresponding values for both first and last answer
select t1.user_id, t2.answer as first_answer, t3.answer as last_answer
from (
select user_id, min(answer_id) min_answer, max(answer_id) max_answer
from yourTable
group by user_id
) t1
join yourTable t2
on t2.answer_id = t1.min_answer
join yourTable t3
on t3.answer_id = t1.max_answer

Limit MySQL Results to One From Each "Group"

Suppose we have a table like the one below.
Id | Name | Group
-----------------
1 | John | 1
2 | Zayn | 2
3 | Four | 2
4 | Ben_ | 3
5 | Joe_ | 2
6 | Anna | 1
The query below will select all of them.
SELECT `Name` FROM `Table` WHERE 1;
How would I select only one person from each group? Who it is doesn't really matter, as long as there's only one name from group 1 and one name from group 2 etc.
The GROUP BY clause isn't fit for this (according to my error console) because I am selecting non aggregated values, which makes sense.
The DISTINCT clause isn't great here either, since I don't want to select the "Group" and definitely not group by their names.
If is not important the resulting name You can anawy leverage some group functions eg with max or min..
leverage the group functions
select max(name) from your_table
group by Group;
otherwise you can use subquery
select name from your_table
where Id in (select min(Id) from your_table group by Group);

Query to Find Duplicate entries

I am looking for an SQL query to give me a list of duplicate entries in a table. However, there are 3 different columns to take into account. First is an ID, Second is a Name, and third is a Date. The situation is that there are multiple Names that are assigned with the same ID, and there are multiple records of those in a day, which makes THOUSANDS of different records per day.
I already filtered it so that only results for the past 7 days will show, but the amount of records is still too much for me to extract. I just want to decrease the number of rows in the output order to properly extract the results.
Sample
|--id-|--name--|-------date------|
| 1 | a |5-9-2015, 10:00am|
| 1 | a |5-8-2015, 10:02am|
| 1 | a |5-8-2015, 11:00am|
| 1 | b |5-8-2015, 10:00am|
| 1 | b |5-8-2015, 10:02am|
| 1 | c |5-8-2015, 10:00am|
| 2 | d |5-8-2015, 10:00am|
expected output
|--id-|--name--|
| 1 | a |
| 1 | b |
| 1 | c |
| 2 | d |
Inclusion of entries without any duplicates are fine. The important thing is to only return a single record of a unique id-name combination for a day.
Thanks in advance for any help that you can give.
You can get the combinations as:
select distinct id, name
from sample;
If you want duplicates, using group by and having:
select id, name
from sample
group by id, name
having count(*) > 1;
EDIT:
If you want this by date, then add date(date) to the group by and perhaps select clauses.
To return single id-name data per day you can use this:
select id, name
from tab
group by id, name, date(date)
The DATE() function extracts the date part of a date or date/time expression.
select id,name
from sample
group by id,name,DATE(date)
having count(*)>1;

How to avoid groups but require a minimum count?

I have answered and read many question on getting the greatest-n-per-group but now find myself needing the opposite.
I have a result set that shows students, date, and project that represent which students worked on a project on a given day.
I would like to see rows where multiple students worked on a project for that day. So if my result set looks like this:
| student | date | project |
+---------+------------+---------+
| 1 | 2014-12-04 | 1 |
| 2 | 2014-12-04 | 1 |
| 3 | 2014-12-04 | 1 |
| 1 | 2014-12-03 | 1 |
I would only like to see the first three rows, so I can see that students 1,2,3 worked together on the same project on the same day. I could filter like this:
GROUP BY date, project
HAVING COUNT(*) > 1
But then only one row will be returned.
you can use your existing query as subquery and get the results
SQL FIDDLE DEMO
SELECT * from Table1 T1
JOIN
(
SELECT date, project
from table1
group by date, project
having count(*) >1
) t
on t1.date = t.date
and t1.project = t.project
This should work.
I think of the table as two sets of data and join them based on date and project and not the same student.
This way if any records exist after the join, we know that they have the same project and date but not for the same student. Group the results ... and you have what you're after.
SELECT A.student, A.date, A.project
from table a
INNER JOIN table b
on A.date=B.Date
and A.Project=B.Project
and a.student<> b.student
group by A.student, a.date, a.project

MySQL query help wanted

I'm relatively new to MySQL, and it's possible that what I want may not be possible to achieve with one single query. I have the following table structure:
name | value1 | value2
I have several records where the names are the same but different values. What I would like to do is to select distinct names, but their values should be added. Here's an example:
john | 1 | 2
jane | 6 | 3
mark | 2 | 5
mark | 3 | 1
So the query (if possible) would return
john | 1 | 2
jane | 6 | 3
mark | 5 | 6
Yes, you can using group by.
Like
select name , sum(value1) as sum1, sum(value2) as sum2 from mytable group by name
You use group by with agregating functions.
SELECT name, sum(value1) as total1,sum(value2) as total2 FROM mytable GROUP BY name
SELECT name, sum(value1) as column1 ,sum(value2) as column2
FROM mytable
GROUP BY name
I recommend to learn a few lessons on MySQL.
Me as a beginner it was very helpful.
$query = "SELECT name, SUM(value1) as v1, SUM(value2) as v2 FROM table GROUP BY name";