Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have two tables:
Family - with columns: Name, ID
Money - with columns: ID, salary, expenses
I have to add each name that his expenses bigger then his salary, the text "spender".
and to all the rest names add the text "frugal".
How to use the Union command to do this?
You shouldn't need a union for this. A join should do:
SELECT
f.Name,
CASE WHEN(SUM(m.Expenses) > SUM(m.Salary)) THEN 'Spender'
ELSE 'Frugal' END AS SomeClass
FROM
Family f
INNER JOIN Money m
ON f.ID = m.ID
GROUP BY
f.Name
SqlFiddle here
Related
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 1 year ago.
Improve this question
This is the query image:
This result should appear as follows:
This is my query: select id, name, total where student.id= marks.sutdent_id
You should group on the student id and name and sum the marks:
SELECT s.id, s.name, SUM(e.marks) AS total
FROM student s
JOIN exam e ON s.id = e.stud_id
GROUP BY s.id, s.name
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
I have a table name customers with two columns, case id and owner. I need to write a query to select random 5 caseids for every name in owner column.please help
For a start, you need something like:
SELECT TOP 5
ID,
[Case ID],
[Owner],
Rnd(-Timer()*[ID]) AS RandomRecord
FROM
[Cases]
ORDER BY
Rnd(-Timer()*[ID]);
to be used as a subquery filtered on OwnerID of your Owners' table.
I once posted an article on this with a lot more details:
Random Rows in Microsoft Access
You can use in:
select t.*
from t
where t.id in (select top 5 id
from t as t2
where t2.name = t.name
order by Rnd(-Timer()*[ID])
);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table called teacher_student_course with teacher_id, student_id, and course_id how would I return a course_id for a course where the student count is lets say above 50?
please help my mind is shot its midnight!
You can group by the course_id and get all group having more than 50 records like this.
SELECT course_id
FROM teacher_student_course
GROUP BY course_id
HAVING COUNT(*) > 50
If you want to check if a course has more than 50 students or not, you need to use a similar query but with a JOIN as shown below.
SELECT tsc.course_id
FROM teacher_student_course tsc
INNER JOIN course ON course.id = tsc.course_id
WHERE course = 'course name'
GROUP BY tsc.course_id
HAVING COUNT(tsc.course_id)>50;
Demo for count greater than 4
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how to get top 1,00,000 customers name and email id who have booked maximum number of tickets? The table has columns like:
NAME, CUST_ID, JOINING DATE, NO. OF TICKETS BOOKED, EMAIL_ID.
Something like the following should work if you are using Microsoft Sql server (tsql)
Select TOP 120 columns FROM table ORDER BY columns desc
SELECT TOP 100000 NAME, CUST_ID, [JOINING DATE], [NO. OF TICKETS BOOKED], EMAIL_ID
FROM YOUR_TABLE
ORDER BY [NO. OF TICKETS BOOKED] DESC
Are you looking for this ?
if you are working with SQL SERVER
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
SELECT staffNO, name, surname, position
FROM Staff s, Branch b
WHERE s.branchNo = b.branchNo AND city = 'London';
I have tried to make the above SQL code into a sub-query and I'm not getting anywhere. Im a beginner in SQL, how do I go about it?
The equivalent query with a sub-query and IN clause would look like this:
SELECT staffNO, name, surname, position
FROM Staff s
WHERE s.branchNo IN (
SELECT b.branchNo
FROM Branch b
WHERE b.city = 'London'
);
This of course assumes that staffNO, name, surname, position are all available as fields on the Staff table. If any of those fields come from Branch then you do need to use the JOIN syntax instead.