I have a SQL exam question that need help
System A :
Student Details
1. Student Full Name
2. Subject Applied
3. Level ( level1, level2 , level 3)
System B:
Students Information
1. Student Full Name
2. Address
3. Tel. No
4. Parent's Name
Now, the project is to create a "System C ", which is a simple software that actually combine both information from System A and System B into System C
Write a SQL query which fulfill the above statement
Thanks
Kelly
Looks like you need to JOIN the two tables together to produce a combined result. Below will combine these two tables using the common field student_full_name.
select
b.student_full_name, b.address, b.number, b.parent_name, a.subject, a.level
from SYSTEM_A as a
inner join SYSTEM_B as b ON a.student_full_name = b.student_full_name
;
If you need to create a new table with the combined result, prefix the above query with CREATE TABLE SYSTEM_C
Related
I am a bit new to the world of oracle and sql and need some guidance. The problem at hand asks me to display name, nickname, and department name for all employees not in department number 20 or 30.
These categories come from different tables as follows.
name from table names01
nickname from table nicks01
deptname from depts01
deptnumber from table depts01
How do you create a SELECT statement to display these 3 field from different tables?
select a.name, b.nickname, .... from names01 a, nicks01 b, .....
The rest is left an exercise for the OP
Note that you referenced 4 tables but your description said 3 tables :)
I create a new view in SQL Server 2008 from two tables that have relation on a field. I want to create a report and do grouping on that common field.
For example:
table1: student(ID,first-name,last-name,phone,address,...)
table2: courses(ID,fk_ID,Course,....)
Now I want to have report that shows all data from both tables with grouping on ID from student table, that must show courses information separated for every student.
my query is:
SELECT TOP (100) PERCENT
dbo.tbl_student.ID,
dbo.tbl_student.firstname, dbo.tbl_student.lastname,
dbo.tbl_courses.Coursename,
dbo.tbl_Courses.CourseDate, dbo.tbl_courses.coursetype,
FROM
dbo.tbl_student LEFT OUTER JOIN
dbo.tbl_courses ON dbo.tbl_student.ID = dbo.tbl_courses.fk_id
ORDER BY
dbo.tbl_student.firstname DESC
But when I create a new report from this view, it shows just one record for every group. I spent 2 hours to solve the problem but I did not succeed.
please help me to create report from two or more tables.
Now it shows one record duplicates for several times for every group
Have you tried a query like this:
SELECT s.[ID], s.[first-name], s.[last-name], s.[phone], ...
c.[ID], c.[Course], ...
FROM student s
LEFT OUTER JOIN
courses c ON s.[ID] = c.[fk_ID]
I am pulling reports for my company and am needing to pull a specific report that I am having trouble with. We are using SQL Server 2012 and I am pulling the SQL reports.
What I need is to pull a simple report:
Group Name, List of Members in the group; Supervisor of the group.
However, the problem is that the supervisor as well as the members and the group name all come from one table in order to get the relevant information. Currently here is my SQL code below:
Use DATABASE
go
-- This is the select portion deciding the columns needed.
select
C.group_name
,C2.first_name
,C2.last_name
-- These are the tables that the query is pulling from.
FROM db..groups AS G
LEFT OUTER JOIN db..contact AS C
ON G.group_id=C.contact_id
INNER JOIN db..contact AS C2
ON G.member=C2.contact_id
go
This pulls the first portion:
The group name, then the first name of a member in that group, and then the last name of a member in that group.
However, I am having trouble getting the supervisor portion. This portion uses the table db.contact under the column supervisor_id as a foreign key. The supervisor_id uses the same unique id as the normal contact_id, but in the same table. Some contact_ids have supervisor_id's that are other contact_id's from the same table, hence the foreign key.
How can I make it so I can get the contact_id that is equal to the supervisor_id of the contact_id that is equal to the group_id?
Taking a quick stab at this while we wait for details
You know you need groups and I'm assuming you don't care about Groups that have no members. Thus Groups INNER JOINed to Contact. This generates your direct group membership. To get the supervisor, you then need to factor in the Supervisor on the specific Contact row.
You might not have a boss, or your boss might be yourself. It's always interesting to see how various HR systems record this. In my example, I'm assuming the head reports to no one instead of themselves.
SELECT
G.group_name
, C.first_name
, C.last_name
-- this may produce nulls depending on outer vs inner join below
, CS.first_name AS supervisor_first_name
, CS.last_name AS supervisor_last_name
FROM
dbo.Groups AS G
INNER JOIN
dbo.Contact AS C
ON C.contact_id = G.member
LEFT OUTER JOIN
dbo.Contact AS CS
ON CS.contact_id = C.supervisor_id;
Depending on how exactly you wanted that data reported, there are various tricks we could use to report that data. In particular, GROUPING SETS might come in handy.
SQLFiddle
My table IncomingLetter has a foreign key to a table Department, which has an ID and a column Short_Name.
I'm using this query to count the incoming letters assigned to a department.
SELECT COUNT(DocumentNumber) AS TotalNumberIncomingLetters
FROM IncomingLetter
WHERE Assigned_To_Department=1;
Whereas this works I want to make a query based upon the short name and not based upon the ID.
SELECT COUNT(DocumentNumber) AS TotalNumberIncomingLetters
FROM IncomingLetter
WHERE Assigned_To_Department.Short_Name="My Department Name";
This does not work, whereas I found examples that are using this syntax. However, it is probably important to notice, that I m using this query in MS access.
You should use
SELECT COUNT(il.DocumentNumber) AS TotalNumberIncomingLetters
FROM IncomingLetter il
INNER JOIN Department d on d.ID = il.Assigned_To_Department
WHERE d.Short_Name="My Department Name";
The "My Department Name" text is actually stored in the Departments table, and only the number (1) is stored in the IncomingLetter table, in the field Assigned_To_Department.
Asking for Assigned_To_Department.Short_Name basically asks the number 1 to get it's Short_Name field, that does not make sense.
You need to tell the database engine two things in these scenarios:
which tables are connected - IncomingLetter and Departments in this case (the inner join part)
how they are connected - by setting their Assigned_To_Department and ID fields respecively (the on ... part
My skills with SQL are simple, so i'm not sure how to do this.. Here it goes.
Lets consider this.
Table A
ID Name LastNameID
1 Jonh 23
2 Helen 19
Table B
ID LastName
23 Bauer
19 Summers
.
SELECT ID Name LastNameID FROM TableA
How can i add another Select inside that one that will return the LastName from Table B based on the LastNameID in Table A.
In this case i usually use a simple function with the Programming language that i use to do that for each returned Row.
But i think its time to do that with SQL alone.
Any help is appreciated.
You just need to join the tables using the LastNameId field:
SELECT TableA.[Name] AS FirstName, TableB.LastName
FROM TableA
INNER JOIN TableB ON TableA.LastNameId = TableB.LastNameId
You are asking "how to do it with a select inside select". Usually, such queries are written in the form of joins, not as sub-selects. Doing joins is better than doing sub-selects for small number of tables.
The query for your application would be
SELECT firstNames.Name, lastNames.LastName FROM TableA firstNames, TableB lastNames
WHERE firstNames.LastNameId = lastNames.LastNameId;
This is the same as writing the query in an explicit join form as shown by Chris in his answer.
There are some good answers here, but if you actually have the first name in one table and last name in another table linked by an ID - I think it is time to review the basics. Start with looking at the Northwind database andmthen some of the database starter kits here.