people the question is to write a query to display the student's first name alone with the course name that they have registered. Sort the result based on student's first name and course name.
Here is my attempt to solve the problem,
select FirstName,CourseName from student s
inner join registration r on s.StudID=r.StudId
inner join course c on r.CourseID=c.CourseID
order by FirstName asc,CourseName asc;
This is the schema for the tables
The output i get when i run the code is this
Where am i going wrong? please help people.
From your comment:
the output seems to be printing two tables as you can see in the picture i think they should be printed in the same table
No, it isn't. You appear to be using SQL/Plus and its all part of the same output; its just that after a certain number of rows SQL/Plus will re-print the column headers.
The commands for SQL/Plus are given here and you should be able to use:
SET PAGESIZE 10000
(Or some other large value) and that will set the number of rows that SQL/Plus will output before it repeats the headers. Then you can re-run your query and the repeated headers will not be printed.
I did this and got the required output.
select FirstName,CourseName from ((student s
inner join registration r on s.StudID=r.StudId)
inner join course c on r.CourseID=c.CourseID)
order by FirstName asc,CourseName asc;
Related
I have this query
SELECT
c.* ,concat ( s.FirstName,'',s.LastName) as FullName
FROM [dbo].[Monitor] c
left join acc.Staff s on s.Id = c.UserId where c.UserId=1
Results:
enter image description here
How to get account information based on last login time in SQL Server.
I don't know how to get account information based on last login time.
From what I understand you want to query the very last login.
SELECT TOP 1 * FROM Monitor m Join Staff s on s.Id = m.UserId
WHERE Object = 'Login' ORDER BY AccessDate Desc
Here is an explanation of the code.
SELECT TOP 1 * FROM Monitor m
The code above is going to query only 1 result (TOP 1) and show all the columns (*) from the table Monitor. If you wish to get only specific columns, you can change * to whatever columns needed. I've given the table Monitor the alias m, because the word starts with that letter, but you can name your alias however you please, for as long as you remember it, or it's easy to realize what column it refers to.
Join Staff s ON s.Id = m.UserID
I've used Join, because you haven't really specified what exact result you are expecting, your question is more about getting the last login, so whatever join is used depends on your expectations. The same goes with the columns I've joined the two tables on. I just copied yours, but they would depend on demanded result and obviously if you have a foreign key in any of the tables, then use that key to join them.
WHERE Object = 'Login' ORDER BY AccessDate DESC
This is the important part of the code for your question. By specifying that we only need rows WHERE the column Object has value of 'Login', we are making sure that only Logins, are shown and all the Logouts are excluded. With ORDER BY AccessDate DESC, we are making sure that the biggest date value is at the top. The way dates work in SQL Server, if you compare two dates, the later date is considered bigger, so the last login would be at the very top, and since we have SELECT TOP 1 at the beginning we are making sure that we are going to get only the very last row.
Before start, Sorry some of results and datas are written in Korean.
Here is a code that I currently am looking on.
SELECT S.*, D.DNAME
FROM STUDENT S, DEPARTMENT D
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT SUBSTR(JUMIN,7,1),MAX(WEIGHT)
FROM STUDENT GROUP BY SUBSTR(JUMIN,7,1))
AND S.DEPTNO1 = D.DEPTNO;
And here is the DEPARTMENT data.
And this is the STUDENT data.
I got a result as I want. But I have some questions when I change this part of the code
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT SUBSTR(JUMIN,7,1),MAX(WEIGHT)
into this one
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT
S.SUBSTR(JUMIN,7,1),MAX(S.WEIGHT)
What I do is simply put S infront of JUMIN and WEIGHT in line3.
But when I do this it shows me the whole data.
I thought JOIN ALIAS (which are S,D in this code) is used as way show that I have two tables to use that is labeled with S and D. S means this data is in STUDENT and D is in DEPARTMENT.
But I think I get it in a wrong way.
Anyway I have no idea how this result is come out.
This one is referring to your main table not the subquery table.
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT
S.SUBSTR(JUMIN,7,1),MAX(S.WEIGHT)
Your original query is already correct. In which you already have a new result set based on your aggregation.
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT SUBSTR(JUMIN,7,1),MAX(WEIGHT)
so I have been given an image and you have to create an output from your table via SQL. I managed to display the Employee Names with the assigned Manager Names but the order of the names did not match the question.
I am thinking ORDER BY WHERE may be used?
Do you think this will help because I don't think I'm implying this correctly maybe
https://sqlandme.com/2013/11/18/sql-server-custom-sorting-in-order-by-clause/
REQUIRED:
SELECT CONCAT(CONCAT(CONCAT(CONCAT(m.TITLEOFCOURTESY,' '),m.FIRSTNAME),' '),m.LASTNAME) AS "Manager Name",CONCAT(CONCAT(CONCAT(CONCAT(a.TITLEOFCOURTESY,' '),a.FIRSTNAME),' '),a.LASTNAME) AS "Employee Name"
FROM EMPLOYEES a
INNER JOIN EMPLOYEES m
ON a.REPORTSTO = m.EMPLOYEEID;
MY OUTPUT:
STEVEN should be third but he is actually second.
If you want to order by the employee name you can paste the same condition in the select to the order by clause.
Judging by the expected output it looks like it is also ordered by salary, department or another column you didn't mention in the question.
my problem is this:
I have a table named
Doctor(id, name, department)
and another table named
department(id, name).
a Doctor is associated with a department (only one department, not more)
I have to do a query returning the department with the maximum number of doctors associated with it.
I am not sure how to proceed, I feel like I need to use a nested query but I just started and I'm really bad at this.
I think it should be something like this, but again I'm not really sure and I can't figure out what to put in the nested query:
SELECT department.id
FROM (SELECT FROM WHERE) , department d, doctor doc
WHERE doc.id = d.id
A common approach to the "Find ABC with the maximum number of XYZs" problem in SQL is as follows:
Query for a list of ABCs that includes each ABC's count of XYZs
Order the list in descending order according to the count of XYZs
Limit the result to a single item; that would be the top item that you need.
In your case, you can do it like this (I am assuming MySQL syntax for taking the top row):
SELECT *
FROM department dp
ORDER BY (SELECT COUNT(*) FROM doctor d WHERE d.department_id=dp.id) DESC
LIMIT 1
You can use Group BY
Select top (1) department.id,count(Doctor.*) as numberofDocs
from department inner join Doctor on Doctor.id = department.id
Group by department.id
Order by count(Doctor.*) desc
I generally avoid using sub queries in MySQL due to a well known bug in MySQL. Due to the bug, MySQL executes the inner query for every single outer query result. Therefore, if you have 10 departments, then doctor query would be executed 10 times. The bug may have been fixed in MySQL 5.6. In this particular case the number of departments may not be large, therefore performance may not be your main concern. However, the following solution should work for MySQL and much more optimized. The answer by dasblinkenlight is almost the same, just got ahead of me :). But MySQL does not support the command top.
select dep.id, dep.name, count(doc.id) as dep_group_count from Doctor doc join department dep on doc.department = dep.id group by doc.department order by dep_group_count DESC LIMIT 1
Using ORMLite, I want to count the number of database items that fit certain criteria. A simplified version of my database is as follows:
Employee Table:
employeeId
departmentId
Department Table:
departmentId
Salary Table:
employeeId
payGrade
Suppose I want to count the number of employees from a subset of departments that are at certain pay grade. When I try something like the following, I get an exception reporting that only one select column is allowed.
salaryQB.selectColumns(salaryQB.EMPLOYEE_ID);
salaryQB.where().eq(salaryQB.PAY_GRADE, 12);
employeeQB.where.in(Employee.DEPARTMENT_ID, departmentList)
.and().in(employeeQB.EMPLOYEE_ID, salaryQB);
employeeQB.setCountOf(true);
count = dao.countOf(employeeQB.prepare());
But code like this returns the following error:
RuntimeException (java.sql.SQLException: Inner query must have only 1
select column specified instead of 2)
Is there any way to do this short of writing a raw SQL query?
Hrm. I can't see any problems with your code #Jeff. I've improved the exception message in ORMLite to show the actual field names for the future but that won't help you right now:
Inner query must have only 1 select column specified instead of 2: [id, foreign]
The message is [obviously] trying to tell you that you have specified more than one selectColumns(...) in the salaryQB inner QueryBuilder. But you seem to be only selecting one column here:
salaryQB.selectColumns(salaryQB.EMPLOYEE_ID);
I don't see where the salaryQB is defined but maybe there is some more code somewhere else that also is using selectColumns? I've tried to reproduce the error in the testInnerCountOf() method towards the bottom of the QueryBuilderTest unit test but it seems to work fine.
If you can reproduce this in a unit test or if you can see how my unit test differs from your config then let me know.
Edit:
As of version 4.22 (from 9/2012), ORMLite now supports simple JOIN statements. So your query can be simplified to:
salaryQB.where().eq(salaryQB.PAY_GRADE, 12);
employeeQB.join(salaryQB);
employeeQB.where().in(Employee.DEPARTMENT_ID, departmentList);
employeeQB.setCountOf(true);
count = dao.countOf(employeeQB.prepare());