Oracle multiple table Joining - oracle11g

I want to know how we can List the name of students and teachers according to classes?
Select
TC.Class_ID,
S.Sudent_Name,
T.Teacher_Name,
C.Class_Type
From Class C
INNER JOIN Teach TC on C.Class_ID=TC.Class_ID
INNER JOIN Student S on TC.Student_ID=S.Student_ID
INNER JOIN Teacher T on TC.Teacher_ID=T.Teacher_ID
Group BY TC.Class_ID
ORDER BY TC.Class_ID;
This is my code which doesnot work.

Related

Using querys to get information from different tables

I am learning SQLite and I'm using this database to learn how to correctly use querys but I'm struggling specially when I have to use data from multiple tables to get some information.
For example, with the given database, is there a way to get the first name, last name and the name of songs that every customer has bought?
All you have to do is a simple SQL SELECT query. When you say you're having trouble getting it from multiple tables, I'm not sure if you're trying to get the data from all of the tables in one single query, as that is not necessary. You just need to have multiple instances of the SELECT query, just for different tables (and different column names).
SELECT firstName, lastName, songName FROM table_name
You have to study about JOINS:
select
c.FirstName,
c.LastName,
t.Name
from invoice_items ii
inner join tracks t on t.trackid = ii.trackid
inner join invoices i on i.invoiceid = ii.invoiceid
inner join customers c on c.customerid = i.customerid
In this query there are 4 tables involved and the diagram in the link you posted, shows exactly their relationships.
So you start from the table invoice_items where you find the bought songs and join the other 3 tables by providing the columns on which the join will be set.
One more useful thing to remember: aliases for tables (like c for customers) and if needed for columns also.
You need to use joins to get data from multiple tables. In this case I'd recommend you using inner joins.
In case your are not familiar with joins, this is a very good article that explains the different types of joins supported in SQLite.
SQLite INNER JOINS return all rows from multiple tables where the join
condition is met.
This query will return the first and last name of customers, and the tracks they purchased.
select customers.FirstName,
customers.LastName,
tracks.name as PurchasedTracks from invoice_items
inner join invoices on invoices.InvoiceId = invoice_items.InvoiceId
inner join customers on invoices.CustomerId = customers.CustomerId
inner join tracks on invoice_items.TrackId = tracks.TrackId
order by customers.LastName

select query with sum value from another table

I'm working with SQLite and have 3 tables, project, invoice and order. I have a select statement based on the project table which picks off the fields stored in this table, however I would also like the sum value of invoices for a particular project and the sum value of orders for a particular project. I know how to get all the values individually, however I haven't figured out how to get the Sum value of orders/invoices into the project data row. Is it a nested select I need to search for, or another concept?
Select based on project table
SELECT project.projectID, project.project_title, project.end_date, project.project_manager, project_status.project_status
FROM project
LEFT JOIN project_status
ON project.project_statusID=project_status.project_statusID
WHERE project.project_statusID BETWEEN 1 AND 12
Select based on invoice table where x would be the project id from the first select
SELECT sum(invoice_net)
FROM invoice
WHERE projectID= x
Select based on order table where x would be the project id from the first select
SELECT sum(total_order)
FROM order
WHERE projectID = x
You didn't provide sample data or even enough of your DB schema for an answer to provide the exact SQL you need.
You don't describe the invoice or order tables. I have to assume they contain a ProjectID attribute.
You could use a subselect:
http://www.techrepublic.com/article/use-sql-subselects-to-consolidate-queries/1045787/
https://msdn.microsoft.com/en-us/library/ff487138.aspx
You're select might look something like (untested):
SELECT
project.projectID,
project.project_title,
project.end_date,
project.project_manager,
project_status.project_status,
(SELECT sum(invoice.invoice_net) FROM invoice WHERE invoice.projectID = project.projectID),
(SELECT sum(order.total_order) FROM order WHERE order.projectID = project.projectID)
FROM project
LEFT JOIN project_status
ON project.project_statusID=project_status.project_statusID
WHERE project.project_statusID BETWEEN 1 AND 12
Or a Join with a Group By. See:
SQL JOIN, GROUP BY on three tables to get totals
You'll have to group by all the non-aggregated (in general this will be all the fields you select from your project table), then apply an aggregate function (sum) to your detail attributes (invoice_net and total_order).
Like this (untested):
SELECT
project.projectID,
project.project_title,
project.end_date,
project.project_manager,
project_status.project_status,
sum(invoice.invoice_net),
sum(order.total_order)
FROM project
LEFT JOIN project_status
ON project.project_statusID=project_status.project_statusID
LEFT JOIN invoice
ON project.projectID = invoice.projectID
LEFT JOIN order
ON project.projectID = order.projectID
WHERE project.project_statusID BETWEEN 1 AND 12
GROUP BY project.projectID, project.project_title, project.end_date,
project.project_manager, project_status.project_status
Which to choose? Performance should be comparable, but it's possible a weakness in the query optimizer could lead to one being favored over the other. Test performance if this is important.
For a one-off, I'd probably use the subselect, if a view of the joined tables didn't already exist. But in practice a view does or should exist, in which case using that (or creating the view, if needed), with GROUP BY, is pretty natural.
So the real answer becomes (untested):
CREATE VIEW project_order_invoice AS
SELECT
project.projectID,
project.project_title,
project.end_date,
project.project_manager,
project_status.project_status,
invoice.invoice_net,
order.total_order
FROM project
LEFT JOIN project_status
ON project.project_statusID=project_status.project_statusID
LEFT JOIN invoice
ON project.projectID = invoice.projectID
LEFT JOIN order
ON project.projectID = order.projectID
SELECT
projectID,
project_title,
end_date,
project_manager,
project_status,
sum(invoice_net),
sum(total_order)
FROM project_order_invoice
WHERE project_statusID BETWEEN 1 AND 12
GROUP BY projectID, project_title, end_date,
project_manager, project_status
For the view, you'll probably want to include all the attributes from all the tables, except only 1 copy of the ID attributes.

T-SQL: Select foreign key from one table and find corresponding rows in another

I have two tables: tblA contains a foreign key which I want to use to pull corresponding rows from tblB. The following queries will not work, but they explain what I want to do:
SELECT [MyID]
FROM [tblA]
SELECT [MyColumn]
FROM [tblB]
WHERE [ID] = [tblA].[MyID]
This should be a fairly simple query but I am a noob with T-SQL.
Use a JOIN:
SELECT A.MyID, B.MyColumn
FROM tblA A INNER JOIN tblB B
ON A.MyID= B.ID
There are different types of joins, the INNER JOIN returns only rows from tblA with a matching key in tblB.

How to fetch data from two different tables?

Good noon to every one
my query is that i have one table name Purchase&Sales and two different field
Purchase
Sales
the data which will be in Purchase text box will be fetch from Total purchase table
and the data will be in sales table will be fetch from Total Sales Table
means the both Value will come from different table to one table
So please Give me a syntax or some idea
Hoping for your Great and positive response
select sum(Purchase) Result from PurchaseTable
union all
select sum(Sales) Result from SalesTable
Using JOIN or try with ForeignKey concept if any.
SELECT
S.Total, -- selecting from one table
P.Total -- selecting from another table
FROM
Sales S
INNER JOIN -- inner join if you can or similar
Purchase P
ON
S.PurchaseId = P.ID
see here for more info http://www.techrepublic.com/article/sql-basics-query-multiple-tables/

Combining data from two SQL queries

I'm porting my app from Django to ASP.NET Webforms (against my will, but what can we do with the corporate world..), and I'm used to Django generating all my SQL queries so now I need help.
I have 3 tables: proceso,marcador,marcador_progreso
Every proceso has many marcador_progreso, which in turn is the foreign key table to marcador.
So basically the tables look like:
proceso
id
marcador
id
text
marcador_progreso
id
marcador_id
proceso_id
state
For all the marcador_progreso where its proceso_id is the current proceso (from a QueryField in the URL), I need to list its state and it's respective marcador.text.
I've been working with EntityFramework but this is like a double query so I'm not sure how to do it.
I guess it is something that combines the following two statements, but I'm not sure how to do it.
SELECT [state] FROM [marcador_progreso]
SELECT [text] FROM [marcador] WHERE ([id] = marcador_id)
You want to do a JOIN:
SELECT mp.state, m.text
FROM marcador_progreso as mp
INNER JOIN marcador as m
ON mp.marcador_id = m.id
This is an excellent post that goes over the various join types.
You'll want to know about JOINs to call more than one table in your FROM clause. JOIN combines records from two or more tables in a database by using values common to each. There are different types - the SQL example below is an INNER join, which gets only records where both of the tables have a match on the common value. You may want to consider a LEFT join which would get any records that exist for the LEFT table (in this case marcador), even if there are not any matching record in the RIGHT(marcador_progreso ) table.
Pop the below in Management Studio, Play with different joins. Replace the INNER with LEFT, run it without the WHERE.
Read about `JOIN's.
In general, for your new venture of writing your own queries, they all start with the same basic structure:
SELECT (UPDATE,WHATEVER DML statement, etc) (COLUMNS) what you want to display (update,etc)
FROM (TABLE) where those records live
WHERE (FILTER/LIMIT) conditions that must be met by the data
Happy fetching!
SQL:
DECLARE #ProcessoId int
SET #ProcessoId = --1
SELECT m.[STATE],mp.[TEXT]
FROM marcador M
INNER JOIN marcador_progreso MP ON MP.marcador_id = m.id
WHERE proceso_id = #ProcessoId
EF INNER example
var marc = from m in yourcontext.marcador
join mp in yourcontext.marcador_progreso on m.id equals mp.marcador_id
where proceso_id == processoIdvariable
EF LEFT example
var marc = from m in yourcontext.marcador
join mp in yourcontext.marcador_progreso on m.id equals mp.marcador_id into details
from d in details.DefaultIfEmpty()
where proceso_id == processoIdvariable

Resources