SQL display MAX value not displaying correctly - sql

I need to display the highest height, first name,and last name of a patient from my table where gender is female. The issues is it is not displaying the highest height. It shows all Females. What am I doing wrong?
SELECT MAX(PatientHeight) as PatientHeight, FirstName, LastName
FROM Patients
WHERE Gender = 'F'
GROUP BY FirstName, LastName
Edit:
I am adding the solution incase someone find this and wants to know more. This was back when I was just learning SQL and had no one I could ask so I posted it here. I want to thank User Jon Ekiz For briefly explaining and better way of approaching the problem. His solution below was exactly was I need to better understand SQL

You can't group by first name and last name and expect just one result. Use this instead:
select firstname
, lastname
, patientheight
from patients
where patientheight = (
select max(patientheight) max_height
from patients
where gender = 'F'
)
and gender = 'F'
BEtter solution is to use row_number and get the first result but not sure which dbms you are using.

Your question isn't clear enough, but following query should return what you're looking for. Question: What if multiple females have same height and are the tallest?
SELECT top 1 PatientHeight, FirstName, LastName
FROM Patients
WHERE Gender = 'F'
Order by PatientHeight desc

Does this do it?
SELECT TOP 1 PatientHeight,FirstName,LastName
FROM Patients
WHERE Gender = 'F'
ORDER BY PatientHeight DESC

Related

Using CASE statement along with SELECT in SQLite to exclude displaying certain results

I was trying to solve the problem of "The number of people of each gender there are in the database, where the gender is known, display the count of people against each gender" in SQLite. I typed a query
SELECT sex, COUNT(name) from employee_table GROUP BY sex ORDER BY sex DESC;
which is partially correct as it showed as output the number of males and females. But, it also shows the number of blank entries which were neither classified as Male or Female.
I have to stop displaying the count of the blank entries. I guess for this, I have to use a CASE statement. I tried CASE statement in different ways. But those didn't work. I tried searching the internet and stack overflow and tried those methods. Those also didn't work.
Any help would be appreciated.
Thanks,
Richard
You want to filter the results and a case expression doesn't do that. Without sample data, it is difficult to say exactly what the expression is, but perhaps:
SELECT sex, COUNT(name)
FROM employee_table
WHERE sex IS NOT NULL
GROUP BY sex
ORDER BY sex DESC;
If the "blank" results are really empty strings, then that would be:
WHERE sex <> ''
This also filters out NULL values.
Or if you want explicit values, then you can use IN:
WHERE sex IN ('m', 'f')

Clarification on database architecture question

I'm working on a quiz for this internship and one question is worded strangely. I'm hoping that one of you could help me find clarification.
Context: I've just created a flat-file table (database), and added 4 columns (UserId,firstname,lastname,email). I filled each column with 15 rows of made-up data.
The question states "Query all rows in the firstname column that start with the letter B and ordered by the lastname column in descending order."
I'm not sure what they're asking for, does this make sense to you?
The question is asking you to query the table, selecting the firstname column and all data that starts with the letter 'B'. And then ordered by the lastname. It's a fairly basic query:
SELECT t.firstname
FROM tablename t
WHERE t.firstname like 'B%'
ORDER BY t.lastname DESC;
EDIT: I did forget the DESC;
The query consists in retrieving first names and last names of rows where the firstname starts with the letter B, and then to order the resulting set using the lastname field in descending order.
The query in sql would be like:
SELECT firstname, lastname FROM users WHERE firstname LIKE 'B%' ORDER BY lastname DESC;

SQL: A Count that merges/joins two columns

A company wants to know what has happened since the beginning of 1999 on hiring. The human resources manager has asked you to
produce a count of the employees hired since then, broken down by both age and gender simultaneously (i.e. 17 males, 25 females, etc.). Write a query that does that.
This is what I have so far. I couldn't figure out how to merge two columns simultaneously. Any thoughts?
SELECT EmployeeID, COUNT(*) AS "Number of employees"
FROM Employee
WHERE Age and Gender
GROUP BY EmployeeID
HAVING COUNT(*) BEGIN = 1999
I am not pretty much sure about your requirement but the right way to write a query is as following and this may fulfill your requirement with slight changes...
SELECT Gender, COUNT(*) AS "Number of employees", Age
FROM Employee
WHERE year(column_name) >= 1999 --Give a real date column here
GROUP BY Age, Gender
Please provide table structure and complete desired output for the exact answer.

Extract info from one table based on data from antoher

I am kind of new to SQL and I made a couple of tables to practice. The columns may have some unrelated categories but I don't know what else write...
Anyway, basically what i want to do is get info from two tables based on the first and last name from one table.
Here are my tables:
Order
Host
I want create a query to pull the ticket number, height, order, subtotal and total by first and last name. The only orders I want to pull are from John Smith And Sam Ting. So in the end, I want my extraction to have the following columns:
Ticket Number
First Name
Last Name
Height
Order
Subtotal
Total
Any help or direction would be awesome!
With the assumption the tables both have unique Ticket_Numbers and that will provide a one-to-one mapping between then.
SELECT
Order.Ticket_Number,
First_Name,
Last_Name,
Height,
Order,
Subtotal,
Total
FROM Order
JOIN Host on Host.Ticket_Number = Order.Ticket_Number
WHERE
(First_Name = 'John' AND Last_Name = 'Smith')
OR (First_Name = 'Sam' AND Last_Name = 'Ting')
You need to "call" the table name first, and then the column. After that you need to use the "join" for the 2 tables. And finally you need the "where". I didn't look for the details so you need to check the "names".
SELECT Order.Ticket_Number, Order.First_Name, Order.Last_Name, Order.Height, Order.Order, Cost.Subtotal, Cost.Total
FROM Order
INNER JOIN Cost
where First_Name="Jhon" and Last_Name="blablabla"
or
First_Name="SecondGuy" and Last_Name="blablabla"

SQL subqueries - which order do you perform the SELECT statements?

I just started learning SQL a couple of days ago, and I'm trying to understand which order I should use the SELECT statement when building subqueries. Here is a simple example of what I am trying to accomplish.
I have two tables - One that specifies the demographics of a customer list, and the other that details how they heard about our company.
In the first table, my customers are listed as either Male or Female in the Gender column, and then in the next column, their ethnicity is specified (Caucasian, African American, Hispanic, etc).
In the second table, there is a Referral column that specifies how they heard about our company (TV, radio, website, etc).
I want to first filter the customers by gender (I want to show only Female data), and then I want to count how many times our customers found us through our website for each ethnicity listed in the table.
SELECT Ethnicity, COUNT(Referral)
FROM Demographics, Marketing
WHERE Demographics.id = Marketing.source_id
AND Referral = 'website'
/* confused about how to put subquery here saying Gender = 'Female' */
ORDER BY Ethnicity
Basically, I'm confused about how to properly include the subquery and if I am even filtering in the correct order.
But here is what I want my table to look like:
/*Data is shown for ONLY Females */
Referral Caucasian African American Hispanic Asian
website 7 19 14 22
I'm sorry, this code is probably really messed up. Please help if you can.
From what you've described, you don't need a subquery:
SELECT Ethnicity, COUNT(Referral)
FROM Demographics, Marketing
WHERE Demographics.id = Marketing.source_id
AND Referral = 'website'
AND Gender = 'Female'
ORDER BY Ethnicity
...note that this gives you a different resultset from the one you've shown, though, with ethnicity, count on each row.
When you are using two table u must use the INNER JOIN syntax:
http://w3schools.com/sql/sql_join_inner.asp
SELECT Ethnicity, COUNT(Referral)
FROM Demographics INNER JOIN Marketing
...
AND Referral = 'website'
AND Gender = 'Female'
...