I'm taking a SQL class and I need help with a question.
I have the following table on a phpmyadmin server
patient (id, age)
with a total of 100 patients
I'm told to find the sum of people that have same age using SQL.
I wrote this query:
SELECT COUNT(age)
FROM patient
HAVING (COUNT(age) > 1);
but it returns 100 as a result and when I did this query and found the number of patients who have the same age for each age and calculated the count manually I found 78 and I checked it manually and it's indeed 78.
SELECT COUNT(*)
FROM patient
GROUP BY age
HAVING (COUNT(*) > 1);
What's wrong with my code?
Start with a subquery that gets the counts of people with each age. Then filter this to counts more than 1 so it's just people with the same age.
Then in the main query you use SUM() to add all these counts together, to get the total number of patients.
SELECT SUM(count)
FROM (
SELECT COUNT(*) AS count
FROM patient
GROUP BY age
HAVING count > 1
) AS x
select age, count(*) from patient group by age;
I need to write SQL query for "average number of particular product sold by date. On each day is sold min one product".
SELECT AVG (COUNT (PID))
FROM SOLD
GROUP BY DATE, PID;
P.S. PID means Product ID.
Is this query okay?
Should this give right answer?
Consider Using distinct count of date columns
SELECT PID,
COUNT(PID)/COUNT(distinct date_) as "Avg.Product Sold By Days"
FROM SOLD
GROUP BY PID;
You can try this sql query below. Basically, it will return the average number of 'SALES' for each product you have. It will group by each distinct product ID. Please provide us the data structure your of table and etc.
SELECT product_ID, trans_date
Sum(sales_of_product) / COUNT(DISTINCT sold_transaction) AS 'avg'
FROM SOLD
GROUP BY product_ID
I just started to learn SQL and I have some queries to write to practise.
Can you help me and check it if it's correct?
I don't have an idea how to create three at the end
Let's assume that database is arbitrary. I just need to learn how to create those kind of queries to work on many diffrent databases.
-Specify the time from the system clock and the system clock date.
SELECT SYSDATETIME(),SYSDATETIMEOFFSET();
-Select employees that are in the age from 30 to 50 years.
SELECT * FROM employees
WHERE dateOfBirth BETWEEN DATE_SUB(NOW(), INTERVAL 30 YEAR)
AND DATE_SUB(date, INTERVAL 50 YEAR);
-Indicate in which department the employee works.
select employees.name, departments.name from employees where
employees.id_department=departments.id_department;
-Specify in what department the employee works and order in descending order and ascending order by name
select employees.name, departments.name from employees where
employees.id_department=departments.id_department ORDER BY Department DESC, Surname ASC;
-Specify in which department the employee works and order in descending order and ascending order by name limiting the number of tuples to chapters beginning with a or s.
-Is there a business department where no one is working?
View your net worker's salary, calculate your 18% tax rate and give you a tax deduction.
I'm new to this also. Would assume something like below:
select employees.name, departments.name from departments
Left Outer Join departments on departments.id_department = employees.id_department
WHERE employees.name IS NULL
I am working on a SQL task. The goal is to know how many flights there are on average, for a given day in a given month from the flights table.
Input table:
flights
id BIGINT
dep_day_of_week varchar (255)
dep_month varchar (255)
dep_date text
An example of the flights table. There could be multiple entries for the same date.
id dep_day_of_week dep_month dep_date
1 Thursday January 4/7/2005 15:24:00
2 Friday February 5/6/2005 12:12:12
3 Friday February 5/6/2005 15:12:12
I read a solution as following:
SELECT a.dep_month,
a.dep_day_of_week,
AVG(a.flight_count) AS average_flights
FROM (
SELECT dep_month, dep_day_of_week, dep_date,
COUNT(*) AS flight_count
FROM flights
GROUP BY 1,2,3
) a
GROUP BY 1,2
ORDER BY 1,2;
My question is in the subquery which calculate the number of flights per day:
SELECT dep_month, dep_day_of_week, dep_date, COUNT(*) AS flight_count
FROM flights
GROUP BY 1,2,3
Since dep_month, dep_day_of_week, dep_date are three correlated attributes, with the dep_date might be the most detailed resolution of the three. So I thought GROUP BY 1,2,3 will do the same function as GROUP BY 3.
To examine what could be the possible differences, I use count(*) from ... to select all the terms resulted from the above subquery,
Select count(*) from (
SELECT dep_month, dep_day_of_week, dep_date, COUNT(*) AS flight_count
FROM flights
GROUP BY 1,2,3 or Group Group by 3)
In the output, the counts for GROUP BY 1,2,3 and GROUP BY 3 , are 447 and 441, respectively. Why there is any difference between these two grouping methods?
Updates:
Thanks to #trincot excellent answer. I use his suggested codes and found inconsistency in the input database.
SELECT dep_date, count(distinct dep_month), count(distinct dep_day_of_week)
FROM flights
GROUP BY dep_date
HAVING count(distinct dep_month) > 1
OR count(distinct dep_day_of_week) > 1
Output:
dep_date count(distinct dep_month) count(distinct dep_day_of_week)
1/16/2001 1 2
10/25/2003 1 2
2/23/2000 1 2
3/29/2001 1 2
4/3/2001 1 2
5/13/2000 1 2
Specifically, the database assigns Monday for 1/16/2001 8:25:00 and Tuesday for 1/16/2001 7:56:00. That is the reason of the inconsistency.
As the date field has a time component, the count(*) in your subquery is going to be 1 every time, since the time component will be different and generate a new group. Your groups are actually per second.
You could get your results without subquery, like this:
select dep_month,
dep_day_of_week,
count(*) /
count(distinct substring_index(dep_date, ' ', 1)) avg_flights
from flights
group by dep_month,
dep_day_of_week
This counts all the flight records, and divides that by the number of different dates these flights are on. The date is extracted by only taking the part before the space.
Note that this means that when you don't have a record at all for a certain date, this day will not count in the average and might give a false impression. For instance, if in January there is only one Friday for which you have flights (let's say 10 of them), but there are 4 Fridays in January, you will still get an average of 10, even though 2.5 would be more reasonable.
About the difference in count
You state that this query returns 447 records:
Select count(*) from (
SELECT dep_month, dep_day_of_week, dep_date, COUNT(*) AS flight_count
FROM flights
GROUP BY 1,2,3)
And this only 441:
Select count(*) from (
SELECT dep_month, dep_day_of_week, dep_date, COUNT(*) AS flight_count
FROM flights
GROUP BY 3)
This seems to indicate that you have identical dates in multiple records, but yet with difference in one of the first two columns, which would be an inconsistency. You can find out with this query:
SELECT dep_date, count(distinct dep_month), count(distinct dep_day_of_week)
FROM flights
GROUP BY dep_date
HAVING count(distinct dep_month) > 1
OR count(distinct dep_day_of_week) > 1
In a healthy data set, this query should return 0 records. If it returns records, you'll get the dates for which the month is not correctly set in at least one record, or the day of the week is not correctly set in at least one record.
What am I doing wrong? Trying to output two COUNTs and need to Group by Grade.
SELECT grade, COUNT(DISTINCT ID) AS total, SUM(case when course like 'AS%'
then 1 else 0 END) Total_As FROM schedule GROUP BY Grade ORDER BY Grade ASC
Only problem is that Total_As is counting grade for multiple instances. Just need to count once if employee took at least one training with Course 'AS%'. The script is currently counting every single 'AS%' taken by employee(ID). Need the "case" part to only count the employee(ID) once to indicate that employee took at least one 'AS%' training or 0 if they did not attend. SUM should not count the employee more than once.