+ 4
MIN function in SQL
I am stuck on the MIN challenge in SQL - titled goal. It seems my issue is that I am having trouble selecting more data from the table beyond just the MIN value. I can get this code to work; SELECT MIN(salary) FROM employees; However when I want to select more data such as name or id, I can’t seem to get the code to work. Where do other values go in the code when the MIN value is used? I am having this issue specifically with the “GOAL” challenge.
11 Respostas
+ 5
1. You should select team, scored goals and conceded goals from the league table according to the result.
==========================
SELECT team, scored_goals, conceded_goals
FROM league
==========================
2. You should use subqueries because aggregate function are not allowed in WHERE.
==========================
SELECT MIN(conceded_goals) FROM league
==========================
3. Order by scored goals in descending order.
==========================
ORDER BY scored_goals DESC;
==========================
4. So, the answer is :
==========================
SELECT team, scored_goals, conceded_goals
FROM league
WHERE conceded_goals = (SELECT MIN(conceded_goals) FROM league)
ORDER BY scored_goals DESC;
========================
+ 5
SELECT
firstname
FROM
staff
WHERE
salary BETWEEN 1500 AND 1900;
+ 1
You can achieve
It by using sub query
Like
Select Id, Name,(Select Min(Salary) From Employee) as Salary From Employee
Where
Select Id, Name From Employee
Provide you result by selecting Id and Name columns from Employee Table
And
(Select Min(Salary) From Employee) as Salary
provide information about minimum Salary by selecting Salary from Employee Table
With the combination of above two query you can get result as you expect
+ 1
Thanks Callum Rushworth I just completed it! Weird it doesn't let me reply to your messages says keep doing the courses lol! Thanks man!
0
Shashank Mishra
That assisted me however now the output is changing all lf the “conceded goals” to be 2? here is my new code:
SELECT team, scored_goals, (SELECT MIN(conceded_goals)FROM league) AS conceded_goals FROM league ORDER BY scored_goals DESC;
The goal is to only display the two teams with the lowest conceded goals and put them in descending order by scored goals
0
Apl wont let me respond to this with a solution
0
it is showing up all the columns with average as 2 dont know why ugh!
0
No problem👍
0
Signal Fish
Perfect Answer...
Wohohooh
0
SELECT firstname FROM staff
WHERE salary BETWEEN 1500 AND 1900 ;
- 2
SELECT team, scored_goals, (SELECT MIN(conceded_goals)FROM league) AS conceded_goals FROM league ORDER BY scored_goals DESC;