I have table fields with student id, and date of birth i want to select oldest student from the table. I just want one student at time. I wrote this query
SELECT studid FROM student ORDER BY dob ASC LIMIT 1;
but it gives me another student id. How to fix this? How to Order table before select in one query?
I also tried ORDER BY dob DESC but the problem is same.
dob data type is datetime
SELECT studid
FROM student
HAVING date of birth = MIN(date of birth)
Or
SELECT studid, MIN(date of birth)
FROM student
Try This
select top 1 studid from student order by dob asc
Related
i have 3 table,
- Employee (idemployee, iddivision, firstname, lastname)
- Salary (idsalary, idemployee, dateadded, amount)
- division (iddivision, divisionname)
i want to display the first name that have the highest amount between january and april
so far i tried
SELECT firstname, MAX(Total) FROM (
SELECT firstname,SUM(amount) AS Total
FROM salary
JOIN employee ON employee.idemployee=salary.idemployee
WHERE dateadded BETWEEN "2019-1-1" AND "2019-4-1"
GROUP BY employee.idemployee) as t
but the employeeid sql show is wrong. why?
SELECT employee.firstname, SUM(salary.amount) AS Total
FROM salary
JOIN employee
ON employee.idemployee=salary.idemployee
WHERE salary.dateadded BETWEEN "2019-01-01" AND "2019-04-01"
GROUP BY employee.firstname
ORDER BY 2 DESC
LIMIT 1
You are filtering by the range you want, then you sum the amounts in that range, grouping by the employee. If you order by that sum and get just the first row, it must he one what you are looking for.
Even better, if your employees just have a name in the firstname attribute, you have the risk to group by the same name wrongly. So, to identify better the employee, I would add the idemployee to the group by sentence. Like this:
SELECT employee.idemployee, employee.firstname, SUM(salary.amount) AS Total
FROM salary
JOIN employee
ON employee.idemployee=salary.idemployee
WHERE salary.dateadded BETWEEN "2019-01-01" AND "2019-04-01"
GROUP BY employee.idemployee,employee.firstname
ORDER BY 3 DESC
LIMIT 1
Do you mean you want it to be ordered from greatest to least?
SELECT firstname, Total FROM (
SELECT firstname,SUM(amount) AS Total
FROM salary
JOIN employee ON employee.idemployee=salary.idemployee
WHERE dateadded BETWEEN "2019-1-1" AND "2019-4-1"
GROUP BY employee.idemployee) as t
order by desc Total
I am trying to write a query to find the 2nd oldest girl student in a class
DOB is date of birth as time stamp
name and gender are varchar
SELECT min(DOB)
FROM Student
WHERE DOB > (SELECT min(DOB) FROM Student where gender='girl')
Is this correct?
Or simpler:
SELECT DOB
FROM Student
WHERE gender='girl'
GROUP BY DOB
ORDER BY DOB ASC
LIMIT 1,1
Grouping by DOB means that two identical DOBs (twins?) that the next lowest is select. It can be omitted in a second identical date counts as the second oldest.
The below query will provide you the desired output
WITH T AS
(
SELECT *
DENSE_RANK() OVER (ORDER BY DOB ) AS Rnk
FROM Student
WHERE gender='girl'
)
SELECT min(DOB)
FROM T
WHERE Rnk = 2;
You can change the RNK value to get the next oldest value, say for third oldest you can use WHERE Rnk = 3
WITH myTableWithRows AS (
SELECT (ROW_NUMBER() OVER (ORDER BY Student.DOB)) as row,*
FROM Student )
SELECT * FROM myTableWithRows WHERE row = 2
I created this fake example and the query result is the following:
Or alternatively you can use this query:
Select top 1 * from (select top 2 from Student order by dob desc) order by dob desc
So I have some results where the query is like
SELECT * FROM employees ORDER BY birth_date ASC;
This simply puts the birth date in ascending order. I want to only display the elder most employees. How can I edit this query to display that
Note : More than 1 employee was born on the same day, therefore i have 6 users who are the eldest but how can I only display the eldest
Maybe with a subquery like:
SELECT * FROM employees WHERE birth_date = (SELECT min(birth_date) FROM employees);
You can combine solutions:
The LIMIT clause is used to specify the number of records to return. Choose one and only SELECT rows with that birth_date.
SELECT * FROM employees where birth_date = (
SELECT birth_date FROM employees ORDER BY birth_date ASC LIMIT 1
);
Database is mysql. The table likes that:
Create table Persons (_id int8 primary key, name varchar, birthday long);
the birthday is the birthday's timestamp.
so I want to select the data that group by date, it means that the result should be one record for one day
select sum(_id) from Persons group by ....
Who can help me, please?
Try this way,
select sum(_id) FROM Persons GROUP BY DATE_FORMAT(birthday, '%Y%m%d')
perhaps:
select sum(_id) from Persons group by DATE_FORMAT(FROM_UNIXTIME(birthday), '%d-%m-%Y')
select sum(_id) from Persons group by date_format(birthday,'%y-%m-%d')
I can't figure this out, I've tried different ways but have been unsuccessful.
My database consists of empid and checkin
I want to find the lowest checkin value which is a number and get the empid for that.
select empid, MIN(checkin) FROM emp GROUP BY empid LIMIT 1
Is what I was doing.
Anyone know how I can do this?
Try this:
select empid, MIN(checkin) FROM emp GROUP BY empid ORDER BY MIN(checkin) LIMIT 1
select empid, checkin from emp order by checkin ASC LIMIT 1