SQL Why put winner at the end while order by year? - sql

From the following table, write a SQL query to find the Nobel Prize winners for the subject not started with the letter 'P'. Return year, subject, winner, country, and category. Order the result by year, descending.
Sample table : nobel_win
Solution:
SELECT *
FROM nobel_win
WHERE subject NOT LIKE 'P%'
ORDER BY year DESC, winner;
I don't understand why the solution put winner at the end. It seems that winner is not required.

Most likely, the winner column was tagged to the end of the ORDER BY clause to break the tie should 2 or more records happen to have the same year. If that be the case, then simply ordering by year descending might leave all records belonging to a given year appearing unsorted. The query you posted above resolves this by ordering all record within a given year by the winner name.

Related

How to get rolling unique count of employees per year based on key fields

I have the following table and I wanted to get the running unique count by dept, team, level. But the cumulative unique count will restart per year.
Note: sorry in my main table example, employee numbers may repeat up to four times. There is another column called leave type but wasn't able to illustrate it in the image
Main table
Expected output would be something like below.
Expected output
Is this possible? Apologies. Not too advanced when it comes to SQL. Thank you.
You can do:
select max(extract(year from date)), date, department, level, team, count(*)
from t
group by date, department, level, team

Order the result by subject, ascending. Why putting subject after `ASC`?

SQL Basic Select Statement: Exercise-24 with Solution
From the following table, write a SQL query to find the details of 1970 Nobel Prize winners. Order the result by subject, ascending except ‘Chemistry’ and ‘Economics’ which will come at the end of result set. Return year, subject, winner, country, and category.
Solution
SELECT *
FROM nobel_win
WHERE year=1970
ORDER BY
CASE
WHEN subject IN ('Economics','Chemistry') THEN 1
ELSE 0
END ASC,
subject,
winner;
SQL Basic Select Statement: Exercise-23 with Solution.
In this solution, year is in front of DESC. I'm confused. Does the order of ASC,DESC and the column title matter? Like the one in exercise 24, both subject and winner are put after ASC. Are they both in ascending order?
In SQL to order the result in ascending or descending, we have to put ASC or DESC keyword after the column name that we need to sort. For example, in Exercise 24 the ASC keyword is after the order by statement. This means the result is in ascending order according to your case.
In exercise 23 the DESC keyword is after the year. This means the result is ordered in descending order considering the year column.
So as a conclusion the keywords ASC and DESC are used after the field name that we want to arrange in order.
Check this link for more details about orders by ASC and DESC.

Get the 10 records having the most recent related elements

I have two tables: agencies and lessons.
An agency can have many lessons.
A lesson belongs to one and only one agency.
A lesson has a date.
I want to get the 10 agencies (or just their ID) having the most recent lessons. In other words I want agencies ordered by their lessons limited to 10 agencies.
I tried various queries with order by and group by but I'm stuck. Everything I try to write is absurd. Here is the first thing I tried:
SELECT
agency_id
FROM lessons
GROUP BY agency_id
ORDER BY start_at;
It fails because:
column "lessons.start_at" must appear in the GROUP BY clause or be used in an aggregate function
I think I understand why. But I'm lost, I don't know how to fix this query. Where should I start?
The order by and select list items must either be part of the group by clause or aggregate functions. You could sort according to the maximum (i.e., last) lesson date:
SELECT agency_id
FROM lessons
GROUP BY agency_id
ORDER BY MAX(start_at) DESC
LIMIT 10

sql practice query issue

Trying to solve some queries from sql zoo to practice sql skills
I have a table nobel with columns (year, subject, winner) which have information on the people who have earned noble prizes in a given year for a given subject.
So I assume the primary key to be composite of (year, subject, winner).
The problem I am trying to solve is: Show winners who have won more than one subject.
The output of the SQL query should have just one column with the winner names.
I feel that I should be using group by and using having(count(winner)) >1. But I think I need to group by by subject and that's where my problem is.
I am not looking for a query. If you can provide me with more logic than query that would help. Also do not worry about the database it needs to be implemented on. I am just practicing these questions.
This should give you people who won on multiple different subjects in any number of years:
SELECT winner
FROM nobel
GROUP BY winner
HAVING COUNT(DISTINCT subject) > 1
For a specific year, just add WHERE year = <whatever> (or GROUP BY year for all years), though no person in history won a Nobel for two subjects in a single year - but who knows what future brings ;)
You should really consider creating a SQLite or MySQL database on your local machine so that you can practice making queries.
The primary key is irrelevant to your question, don't worry about it.
You are thinking along the correct lines - you will need a group by and having clause. If you group by winner, then your proposed query should work. I've added a count of wins.
select
winner, count(winner) as wins
from
nobel
group by winner
having (count(winner)) > 1
SELECT winner
FROM nobel
GROUP BY winner
HAVING COUNT(winner) > 1
This is the simplest way to do it ......

How to produce a distinct count of records that are stored by day by month

I have a table with several "ticket" records in it. Each ticket is stored by day (i.e. 2011-07-30 00:00:00.000) I would like to count the unique records in each month by year I have used the following sql statement
SELECT DISTINCT
YEAR(TICKETDATE) as TICKETYEAR,
MONTH(TICKETDATE) AS TICKETMONTH,
COUNT(DISTINCT TICKETID) AS DAILYTICKETCOUNT
FROM
NAT_JOBLINE
GROUP BY
YEAR(TICKETDATE),
MONTH(TICKETDATE)
ORDER BY
YEAR(TICKETDATE),
MONTH(TICKETDATE)
This does produce a count but it is wrong as it picks up the unique tickets for every day. I just want a unique count by month.
Try combining Year and Month into one field, and grouping on that new field.
You may have to cast them to varchar to ensure that they don't simply get added together. Or.. you could multiple through the year...
SELECT
(YEAR(TICKETDATE) * 100) + MONTH(TICKETDATE),
count(*) AS DAILYTICKETCOUNT
FROM NAT_JOBLINE GROUP BY
(YEAR(TICKETDATE) * 100) + MONTH(TICKETDATE)
Presuming that TICKETID is not a primary or unique key, but does appear multiple times in table NAT_JOBLINE, that query should work. If it is unique (does not occur in more than 1 row per value), you will need to select on a different column, one that uniquely identifies the "entity" that you want to count, if not each occurance/instance/reference of that entity.
(As ever, it is hard to tell without working with the actual data.)
I think you need to remove the first distinct. You already have the group by. If I was the first Distict I would be confused as to what I was supposed to do.
SELECT
YEAR(TICKETDATE) as TICKETYEAR,
MONTH(TICKETDATE) AS TICKETMONTH,
COUNT(DISTINCT TICKETID) AS DAILYTICKETCOUNT
FROM NAT_JOBLINE
GROUP BY YEAR(TICKETDATE), MONTH(TICKETDATE)
ORDER BY YEAR(TICKETDATE), MONTH(TICKETDATE)
From what I understand from your comments to Phillip Kelley's solution:
SELECT TICKETDATE, COUNT(*) AS DAILYTICKETCOUNT
FROM NAT_JOBLINE
GROUP BY TICKETDATE
should do the trick, but I suggest you update your question.