Select Min and Max value - mysql

I have a table called 'customers':
id | name | age
1 | john | 35
2 | paul | 22
3 | ana | 26
4 | mark | 19
5 | jack | 29
i want to select the name and max age, the name and min age... something like:
john 35 mark 19
is it possible?

The below query will give you the min and max on one row as requested. If there are multiple matches for min/max you will get multiple rows. Depending on the SQL engine you use, the syntax to limit to one row is different.
SELECT cMax.Name, cMax.Age, cMin.Name, cMin.Age
FROM customers cMin
JOIN customers cMax ON
cMax.Age = (SELECT MAX(Age) FROM customers)
WHERE cMin.Age = (SELECT MIN(Age) FROM customers)
There are different types of joins (e.g. INNER, OUTER, CROSS); however, for your question it doesn't much matter which you use.

Yes, you can do it,
select name, age from customers
where age in (select max(age)
from customers union select min (age)from customers)

try this
select name, age from customers where age=(select max(age) from customers) union
select name, age from customers where age=(select min(age) from customers)

If you want them on the same row:
select cmin.*, cmax.*
from (select name, age as minage
from customers
order by age asc
fetch first 1 row only
) cmin cross join
(select name, age as maxage
from customers
order by age desc
fetch first 1 row only
) cmax;
fetch first 1 row only is standard syntax for returning only the first row of the result set. Some databases have bespoke syntax, such as limit or select top (1).

Try using this query to show MAX age:-
select * from customers where age=(select max(age) from customers);
To show MIN age use the below Query:-
select * from customers where age=(select min(age) from customers);

You could use a cross join, which will put the two query ouputs next to one another. Building off Rodrigo's queries:
select
max_cust.name,
max_cust.age,
min_cust.name,
min_cust.age
from (select name, age from customers where age=(select max(age) from customers)) as max_cust
cross join (select name, age from customers where age=(select min(age) from customers)) as min_cust
It's maybe not the most performant but it gets the right shape. Be wary of cross joins when the tables don't have exactly 1 row, as it creates a cartesian product of the rows in the tables being joined.

Related

MYSQL count multiple orders without grouping

I'm trying to produce a query that shows every order AND adds an additional column which shows if the customer has previously placed an order by NAME, ADDRESS or USER_ID, without grouping results.
I've tried:
SELECT *, COUNT(`NAME`) c FROM `salesorders` GROUP BY `NAME` HAVING c > 0
This shows all customer orders, however if the customer has previously ordered before, the customers name only shows once due to the grouping.
e.g.
Name, Date, Previous_Orders
John Smith, 2019-11-28, 2
Lisa Thomas, 2019-11-20, 1
I would like it show both occurrences:
John Smith, 2019-11-28, 2
Lisa Thomas, 2019-11-20, 1
John Smith, 2019-05-07, 2
salesorders columns;
Name
User_ID
Date
Lets say the new column is Previous_Orders
For MariaDB 10.4.6 you can use COUNT() window function:
select *,
count(full_add) over (partition by full_add) Previous_Orders
from salesorders;
If the column Name is unique, then you can replace user_id with Name if you prefer.
If by Previous_Orders you mean the number of orders up to the date in the current row, then change to:
select *,
count(full_add) over (partition by full_add order by date) Previous_Orders
from salesorders;
See the demo.
You could use a join with the subquery for count group by name
SELECT s.*, t.c
FROM `salesorders` s
INNER JOIN
(SELECT name, COUNT(*) c
FROM salesorders
GROUP BY name) t ON t.name = s.name
WHERE t.c > 0

Guidance required for sql query

I have a database with one table as shown below. Here I'm trying to write a query to display the names of medication manufactured by the company that manufactures the most number of medications.
By looking at the table we could say the medication names which belongs to the company id 1 and 2 - because those company manufactures the most medication according to this table, but I'm not sure how to write a query for selecting the same i said before.
ID | COMPANY_ID | MEDICATION_NAME
1 1 ASPIRIN
2 1 GLUCERNA
3 2 SIBUTRAMINE
4 1 IBUPROFEN
5 2 VENOFER
6 2 AVONEN
7 4 ACETAMINOPHEN
8 3 ACETAMINO
9 3 GLIPIZIDE
Please share your suggestions. Thanks!
Several ways to do this. Here's one which first uses a subquery to get the maximum count, then another subquery to get the companies with that count, and finally the outer query to return the results:
select *
from yourtable
where companyid in (
select companyid
from yourtable
group by companyid
having count(1) = (
select count(1) cnt
from yourtable
group by companyid
order by 1 desc
limit 1
)
)
SQL Fiddle Demo
This Query might work. I have not tested but the logic is correct
SELECT MEDICATION_NAME
FROM TABLE where
COMPANY_ID=(SELECT
MAX(counted)
FROM ( SELECT COUNT(*) AS counted FROM TABLE ) AS counts);

Filter on minimum number of records (COUNT) matching condition

I would like to select records from a single table based on a minimum number of records (N) for a particular person. For example, if N=2, then
Name Time
John 09:00:00
Sam 09:05:00
Sally 09:06:00
John 09:10:00
Jane 09:15:00
Joe 09:16:00
Sally 09:22:00
filters down to
Name Time
John 09:00:00
Sally 09:06:00
John 09:10:00
Sally 09:22:00
It is easy to count the records but how do I select them?
use group by function.
select count(*),Name from table_name group by Name
Try this :
select *
from mytable
where name in (
select name
from mytable
group by name
having count(1) =2
)
You would first group your records by the number of times a particular person occurs:
select count(1), name
from Person
group by name;
Next, you want to select those persons that occur twice or more:
select name, time
from Person
where id in (
select id from Person group by name having count(1) >= 2
)
Note that I assume your Person table has an id column which uniquely identifies each column.
So, basically, you first select the ids of the persons that occur twice or more, and then select the desired data for those persons.

Select distinct column along with some other columns in MySQL

I can't seem to find a suitable solution for the following (probably an age old) problem so hoping someone can shed some light. I need to return 1 distinct column along with other non distinct columns in mySQL.
I have the following table in mySQL:
id name destination rating country
----------------------------------------------------
1 James Barbados 5 WI
2 Andrew Antigua 6 WI
3 James Barbados 3 WI
4 Declan Trinidad 2 WI
5 Steve Barbados 4 WI
6 Declan Trinidad 3 WI
I would like SQL statement to return the DISTINCT name along with the destination, rating based on country.
id name destination rating country
----------------------------------------------------
1 James Barbados 5 WI
2 Andrew Antigua 6 WI
4 Declan Trinidad 2 WI
5 Steve Barbados 4 WI
As you can see, James and Declan have different ratings, but the same name, so they are returned only once.
The following query returns all rows because the ratings are different. Is there anyway I can return the above result set?
SELECT (distinct name), destination, rating
FROM table
WHERE country = 'WI'
ORDER BY id
Using a subquery, you can get the highest id for each name, then select the rest of the rows based on that:
SELECT * FROM table
WHERE id IN (
SELECT MAX(id) FROM table GROUP BY name
)
If you'd prefer, use MIN(id) to get the first record for each name instead of the last.
It can also be done with an INNER JOIN against the subquery. For this purpose the performance should be similar, and sometimes you need to join on two columns from the subquery.
SELECT
table.*
FROM
table
INNER JOIN (
SELECT MAX(id) AS id FROM table GROUP BY name
) maxid ON table.id = maxid.id
The problem is that distinct works across the entire return set and not just the first field. Otherwise MySQL wouldn't know what record to return. So, you want to have some sort of group function on rating, whether MAX, MIN, GROUP_CONCAT, AVG, or several other functions.
Michael has already posted a good answer, so I'm not going to re-write the query.
I agree with #rcdmk . Using a DEPENDENT subquery can kill performance, GROUP BY seems more suitable provided that you have already INDEXed the country field and only a few rows will reach the server. Rewriting the query giben by #rcdmk , I added the ORDER BY NULL clause to suppress the implicit ordering by GROUP BY, to make it a little faster:
SELECT MIN(id) as id, name, destination as rating, country
FROM table WHERE country = 'WI'
GROUP BY name, destination ORDER BY NULL
You can do a GROUP BY clause:
SELECT MIN(id) AS id, name, destination, AVG(rating) AS rating, country
FROM TABLE_NAME
GROUP BY name, destination, country
This query would perform better in large datasets than the subquery alternatives and it can be easier to read as well.

mysql query to find sum of different names

I use similar queries (10) as following queries (modified) to find sum
SELECT sum(amount) AS amount
FROM `students`
WHERE sex='M'
&& name in ('salil', 'anil', 'gaikwad')
...and:
SELECT sum(amount) AS amount
FROM `students`
WHERE sex='M'
&& name in ('salil1', 'anil1', 'gaikwad1')
i want to make a single query of the above 10 queries. is it possible?
You can use UNION
SELECT 'subset1', sum(amount) AS amount FROM students WHERE sex='M' and name in ('salil', 'anil', 'gaikwad')
UNION
SELECT 'subset2', sum(amount) AS amount FROM students WHERE sex='M' and name in ('salil1', 'anil1', 'gaikwad1')
However, you probably query these sets of students for a reason, perhaps anil, salil and gaikwad are one group of students. If so, you should reflect this in the database structure, not in your code.
You could add a field 'SUbset' or 'Group' or whatever that is, to students table, so it looks like this:
name group_id
salil 1
anil 1
gaikwad 1
salil1 2
...
Then you can do
select group_id, sum(amount) from students group by group_id
Try something like this
SELECT sum(amount) AS amount
FROM students INNER JOIN
(SELECT 'salil%' Val UNION SELECT 'anil%' UNION SELECT 'gaikwad%') s ON students.NAME LIKE s.Val
WHERE sex='M'
This allows you to use the values in the second Table to join with LIKE.