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 :)
Related
I have two different tables with records I need to join together in a way I can't quite figure out how to make work. My data looks like this.
Table A
Columns: Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager ... many other columns
Sample Values:
12345 Department1 Team1 67890 Yes/No
.
.
.
One employee per row, several thousand rows comprising the entire company
Table B
Employee_ID, Manager_ID ... other columns
The exact same data set as Table A
Currently I'm combining those two tables (and three others) with a simple join on Employee_ID, which I'm then using as a data source in Tableau to visualize the data.
What I'd like to do with a SQL script is as follows:
Check to see whether an employee in Table A is a manager or not based on the Is_a_Manager column
If they are, find an employee in Table B who is one of their direct reports by matching the employee ID in Table A to the Manager ID in Table B.
Lookup that direct report's department and team in Table A by matching the Employee_ID in Table B to Employee_ID in Table A and displaying the Employee_Department and Employee_Team columns.
Add the direct report's department and team to two new columns in the original manager's Table A row
I'd like the final output in Table A to be something like
Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager? ... Direct_Report_Department, Direct_Report_Team
Also, an important point is that some managers will have employees who are on different teams, so values in the Direct_Report_Department and Direct_Report_Team are not distinct. I only actually need any one employee's Department and Team to display, it doesn't matter which employee's it is.
Finally, I am able to do step 1 fairly easily in Tableau, so if the SQL script could do steps 2-4 and simply return a null value if the employee was not a manager, that would work for me as well.
Any ideas on how to accomplish this would be greatly appreciated. Thank you!
This should work based on the requirement provided. You don’t have to do any of the steps in Tableau and can simply export the output from the SQL as your data source
Select Tb1.Employee_ID, Tb1.Employee_Department, Tb1.Employee_Team, Tb1.Manager_ID, Tb1.Is_a_Manager, Tb3. Direct_Report_Department, Tb3. Direct_Report_Team
from Table_A Tb1
join (Select Manager_id, max(Employee_id) as emp_id from Table_B group by Manager_id) Tb2
on Tb1.Employee_id = Tb2.Manager_id
left join (Select Employee_ID, Employee_Department as Direct_Report_Department, Employee_Team as Direct_Report_Team from Table_A group by Employee_ID, Employee_Department, Employee_Team) Tb3
on Tb2.emp_id = Tb3.Employee_ID
where Tb1.Is_a_Manager = 'Yes';
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
I have a requirement where I have to fetch data from database table.
Condition is,
There are two tables with identical columns like name, mobile, address, age.
Ex. table names like Student and Teacher.
now if I don't find anything in student table then I will retrieve data from Teacher table.
how to write this query ?
To get results from the two tables you would use UNION ALL. Here however you only want to access table Teacher when there are no matching rows in table Student. This is slightly more difficult. You can still use UNION ALL, but need an EXISTS clause on your teacher query.
select * from student
union all
select * from teacher where not exists (select * from student);
I have 2 tables: person_concern and person. They both have a code column and person has a dept column. I want to select data from person_concern table where the dept column in person table = 30.
Basically the person table has a different code for each row and then the person is put in a department. So i can have multiple rows in the person table with the same dept field.
The person_concern table is for writing problem concerns for people. It has a code to know which person its referring to which id get from the person table. So i want to select data from that person_concern table where the code matches the code from the person table and that person is from a certain dept such as 30.
Hope that makes sense... Here's what ive tried so far but get an invalid number error.
select
PERSON_CONCERNS.CODE
PERSON_CONCERNS.ENTRY_DATE
PERSON_CONCERNS.ENTRY_OPR
PERSON_CONCERNS.DISCUSSION
from PERSON_CONCERNS
inner join PERSON on PERSON_CONCERNS.CODE = PERSON.CODE
where PERSON.DEPT = 30
I think you are just missing the commas on your field names but need to see the actual code you are running along with the create table statements and some sample data to be sure.
select
PERSON_CONCERNS.CODE,
PERSON_CONCERNS.ENTRY_DATE,
PERSON_CONCERNS.ENTRY_OPR,
PERSON_CONCERNS.DISCUSSION,
from PERSON_CONCERNS
inner join PERSON on PERSON_CONCERNS.CODE = PERSON.CODE
where PERSON.DEPT = 30
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.