SQL group by - can it be this simple? [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
can someone please check if this is correct?
Not sure if my answer to Q6 is correct, I am not sure if the group_by I am using is right or not, the rest I think is ok
Thanks

You are close. You need to:
Use COUNT() instead of SUM().
There's no need to use the HAVING clause.
Optionally, you can add aliases to the columns, so they become easier to read.
Your query should look like:
select a.author_id, count(*) as titles, sum(b.quantity_ordered) as units
from a join b on a.book_id = b.book_id
group by a.author_id

Related

A newbie in SQL asking about functions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
How can I find data that dont end with letter 'g' without using NOT LIKE function, please help
select * from table where right(column, 1) != 'g'
Since you asked how to do it using "not like," I'll answer that. The function version provided by #Zoories would be more efficient.
This works at w3schools.com
SELECT * FROM Customers where CustomerName not like '%g';

Is there any way to put WHERE filter inside the SELECT statement in SQL? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I got a task that i need to know how can i put WHERE clause inside the SELECT statement (like SELECT .... WHERE.... FROM...) . hope you guys could help me. thank you :)
'Where' is used to filter the data based on a condition.
The general syntax is:-
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Here you can get a better overview.

How to you use Max and Min expression in Ms Access SQL? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
CrimeData Table for 12 months
Crime Took place in Easternmost
I need to find the following:
Q.7 What type of Crime takes place in the …
(a) Easternmost ………………..
(b) Westernmost ………………..
(c) Northernmost………………..
(d) Southernmost………………..
I tried to find the crime took place in the Easternmost using the following SQL code
SELECT Max(CrimeData.Easting) AS MaxOfEasting, CrimeData.Type
FROM CrimeData
GROUP BY CrimeData.Type;
but I got more than one crime and also other Easting numbers. Can you please tell me if there are other good ways to find the solution.
Please see the attached pictures :)
Rather than using Max/Min, have a look at the TOP keyword in SQL. Some SQL might look like:
SELECT TOP 1 CD.*
FROM CrimeData CD
ORDER BY CD.Easting DESC;
Regards,

Retrieve right year in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Goal:
"In which years was the Physics prize awarded but no Chemistry prize. (WARNING - this question is way too hard for this level, you will need to use sub queries or joins). "
Problem:
Don't know how to solve it.
The code can be found in "http://sqlzoo.net/1b.htm", headline 3a.
SELECT
distinct yr
FROM
nobel
WHERE
yr not in (select yr from nobel where subject in ('Chemistry')) AND
subject in ('Physics')
One option using subqueries. The subquery gets all the years where there was a chemistry prize which is used to to excude those years with the not in statement.
The distinct yr accounts for the fact that there could have been two prizes awarded.
There's a bunch of different ways to make this work. Take the code above and try making a join with the subquery work. That'll give you more practice with subqueries and using them in joins which is a useful thing.

Why does WordPress prefix its column names with the table name? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
I noticed that in a recent update WordPress decided to change all of its column names to include the table name prefix. WHY?!
The wp_posts table now has a column called post_title, for example. It used to just be title. I don't understand the reason for this change. There must've been one, yes?
I just don't understand what possible reason there could be since in SQL you can refer to things like table.column. Also, it must've been really difficult to change all of the code.
Perhaps to prevent confusion when making joins with tables having similar column names.
There is no good reason for doing this, plus it makes maintenance much more difficult.
There is no confusion if columns are specified like:
SELECT p.title, s.title FROM wp_posts p JOIN site s ON (p.site_id = s.id);