If I had the following relation
Staff (staffNo(PK), fName, lName, position, sex, DOB)
How would I go about writing a query which produced a list of all details of all female staff ordered by their second name then their first name?
My guess is:
SELECT * FROM Staff ORDER BY fName ASC, lName ASC WHERE sex = 'f'
Is this correct?
Well, you can try this out to see if this is correct :)
But you should swap where predicate with order by clause, and ordering predicates as well: if you want to sort by the last name at first - you should specify last name first in the order by.
SELECT * FROM staff WHERE sex = 'F' ORDER BY lName ASC, fName ASC
You had fName and lName the wrong way round, and your ORDER BY comes after the WHERE
Try this query:
SELECT * FROM Staff WHERE sex = 'f' order by lName, fName;
Related
SELECT personid
, persfname
, perslname
FROM person
ORDER
BY perslname ('__l%') DESC
This is the code I thought it would be but I am having trouble displaying the correct query.
This will work.
SELECT personid
, persfname
, perslname
FROM person
WHERE perslname LIKE '__I%'
ORDER BY persFname DESC
You can use either REGEXP or SUBSTRING.
Using REGEXP:
SELECT personid, persfname, perslname
FROM person
WHERE perslname REGEXP '^(.{2}l)'
ORDER BY persfname DESC
Using SUBSTRING:
SELECT personid, persfname, perslname
FROM person
WHERE SUBSTRING(perslname, 3, 1)='l'
ORDER BY persfname DESC
Here is my PEOPLE table where I store name, surname, DOB (date of birth) and some other data. In a new query I need to add additional column with the condition for people with the same Name & Surname combination. Here is the condition:
Go through each group of people with the same Name & Surname combinations, in additional field mark those, whose DOB is not maximum and not minimum (compared only to same Name & Surname)
If there is only 1 or 2 occurrences of same Name & Surname, mark them anyway
Here is the result of a query
Explanation:
John Doe marked as met only once
Tom Taylor marked as met only twice
Alice Smith and Bob Brown marked everywhere except records with min and max DOB
Please help to form SQL query for the desired output. Here is my understanding (guessing)
Get list of unique Name&Surname pairs, ( where occurrences >2 ??? )
For each unique pair find rows with min and max DOB (avoid them)
In a new CheckBox column mark those that are left (not extremum)
First group by name, surname to get the number of occurrences and min and max dob of each name and surname and join the results to the table.
With a CASE statement apply the conditions:
select
p.*,
case
when g.counter in (1, 2) then 'mark'
else case
when p.dob not in (g.mindob, g.maxdob) then 'mark'
end
end Checkbox
from peaople inner join (
select
name, surname,
count(*) counter,
min(dob) mindob,
max(dob) maxdob
from people
group by name, surname
) g on g.name = p.name and g.surname = p.surname
Use window functions in MySQL 8+:
select p.*,
(case when count(*) over (partition by name, surname) <= 2
then 'mark'
when row_number() over (partition by name, surname order by dob) > 1 and
row_number() over (partition by name, surname order by dob desc) > 1
then 'mark'
end) as checkbox
from people p;
Note: If there are duplicates for the earliest or latest birthdate, this only excludes one of them. If you want to exclude all of them, use rank() instead of row_number().
I'm trying to display only zipcodes that multiple people have in my table and sort it by ascending order.
I have tried the following but it does not work.
SELECT zipcode AS "zipcodes" FROM people GROUP BY zipcode ORDER BY ASC;
SELECT zipcode AS "zipcodes", count(1) as ZIP_COUNT
FROM people
GROUP BY zipcode
ORDER BY ZIP_COUNT ASC;
If you only need zipcode that appears 2+ times append (before the order) :
HAVING COUNT(1) > 1
Or (if Mysql supports aliases in HAVING clause) :
HAVING ZIP_COUNT > 1
The main issue with your query is that you didn't specify any column in the order by sections.
SELECT zipcode AS "zipcodes" FROM people GROUP BY zipcode ORDER BY ASC;
So just add the zipcode to it and it will be good.
SELECT zipcode AS "zipcodes" FROM people GROUP BY zipcode ORDER BY zipcode ASC;
The following query selects zipcode(s) having multiple entries in table "people":
SELECT zipcode AS "zipcodes"
FROM people
GROUP BY zipcode
HAVING COUNT(*) > 1
ORDER BY zipcode ASC;
try this one
SELECT zipcode AS "zipcodes", count(zipcode) as noofzip
FROM people
GROUP BY zipcode
having count(zipcode)>1
order by zipcode
I am currently taking Database Systems course in University this is a question from one of the exercises ,given by my instructor, that I couldn't figure out. I was able to do other questions. Thanks in advance.(primary keys are in italic)
QUESTION//
Consider the following relational schema.
student(sid, sname, address, city, gpa)
course(cid, cname, iid)
enroll(sid, cid, grade)
instructor(iid, iname)
Give the corresponding SQL queries for each of the following.
Find the id and name of the student with the 10th highest gpa. You can assume, for simplicity, that the gpa values are distinct.
The psedocode I would attempt is select the top ten students with their gpa in descending order. At that point you have ten GPA's in desc (greatest to least) order. The 10th is at the bottom. But how do you get the 10th? You could subquery the result of that query, to get the top value in ascending (least to greatest) order.
A general way to subquery:
SELECT dT.Col1
FROM (
SELECT Col1
FROM Table
--ORDER BY?
) AS dT
--ORDER BY ?
I solved it like this Feel free to make any corrections. Thanks for helpful comments.
SELECT sid, sname
FROM ( SELECT sid, sname
FROM student
ORDER BY gpa DSC LIMIT 10)
ORDER BY gpa ASC LIMIT 1
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