Relational Algebra and SQL Oracle - sql

I need to output data from one table but only if that customers name has shown up in another table and I'm not quite sure how get this to work. Thanks

You can use an INNER JOIN which will return all rows that appear in both tables:
select t1.*
from table1 t1
inner join table2 t2
on t1.name = t2.name
If you need help learning JOIN syntax, then here is a great visual explanation of joins

This is a basic SQL query:
SELECT *
FROM t
WHERE t.name IN (SELECT name FROM t2);
There are other ways to express this. Are you new to SQL?

Related

Selecting from multiple tables in SQL Server

What does this syntax in SQL Server mean?
select top 10 *
from
table1 as t1,
table2 as t2
Is this a union or join? Does it combine two tables together? Not sure exactly what this syntax means? does anyone have online resources for this?
, is archaic syntax for cross join. This is more colloquially written as:
select top 10 *
from table1 as t1 cross join
table2 as t2;
That said, using top without order by is suspicious.

SQL query, where = value of another table

I want to make a query that simply makes this, this may sound really dumb but i made a lot of research and couldn't understand nothing.
Imagine that i have two tables (table1 and table2) and two columns (table1.column1 and table2.column2).
What i want to make is basically this:
SELECT column1 FROM table1 where table2.column2 = '0'
I don't know if this is possible.
Thanks in advance,
You need to apply join between two talbes and than you can apply your where clause will do work for you
select column1 from table1
inner join table2 on table1.column = table2.column
where table2.columne=0
for join info you can see this
Reading this original article on The Code Project will help you a lot: Visual Representation of SQL Joins.
Find original one at: Difference between JOIN and OUTER JOIN in MySQL.
SELECT column1 FROM table1 t1
where exists (select 1 from table2 t2
where t1.id = t2.table1_id and t2.column2 = '0')
assuming table1_id in table2 is a foreign key refering to id of table1 which is the primary key
You don't have any kind of natural join between two tables.
You're asking for
Select Houses.DoorColour from Houses, Cars where Cars.AreFourWheelDrive = '1'
You'd need to think about why you're selecting anything from the first table, there must be a shared piece of information between tables 1 and 2 otherwise a join is pointless and probably dangerous.

comparison with more than one value in sql

I have two tables. In the first one there is a column called id. I want to select some of these IDs, and to choose from the other table the rows that have these IDs. How do I do that?
You need to learn SQL joins.
SELECT SecondTable.*
FROM SecondTable
INNER JOIN FirstTable ON (FirstTable.ID = SecondTable.ID)
WHERE FirstTable.SomeField = 'Something Else'
Here is a good link to get you started
Inner Joins
Basically for your example you would use something like
SELECT table1.id, table1.value, table2.value
FROM table1
INNER JOIN table2
ON table1.id=table2.id

Use join with a table and SQL Statement

Joins are usually used to fetch data from 2 tables using a common factor from either tables
Is it possible to use a join statement using a table and results of another SQL statement and if it is what is the syntax
Sure, this is called a derived table
such as:
select a.column, b.column
from
table1 a
join (select statement) b
on b.column = a.column
keep in mind that it will run the select for the derived table in entirety, so it can be helpful if you only select things you need.
EDIT: I've found that I rarely need to use this technique unless I am joining on some aggregated queries.... so I would carefully consider your design here.
For example, thus far most demonstrations in this thread have not required the use of a derived table.
It depends on what the other statement is, but one of the techniques you can use is common table expressions - this may not be available on your particular SQL platform.
In the case of SQL Server, if the other statement is a stored procedure, you may have to insert the results into a temporary table and join to that.
It's also possible in SQL Server (and some other platforms) to have table-valued functions which can be joined just like a view or table.
select *
from TableA a
inner join (select x from TableB) b
on a.x = b.x
Select c.CustomerCode, c.CustomerName, sq.AccountBalance
From Customers c
Join (
Select CustomerCode, AccountBalance
From Balances
)sq on c.CustomerCode = sq.CustomerCode
Sure, as an example:
SELECT *
FROM Employees E
INNER JOIN
(
SELECT EmployeeID, COUNT(EmployeeID) as ComplaintCount
FROM Complaints
GROUP BY EmployeeID
) C ON E.EmployeeID = C.EmployeeID
WHERE C.ComplaintCount > 3
It is. But what specifically are you looking to do?
That can be done with either a sub-select, a view or a temp table... More information would help us answer this question better, including which SQL software, and an example of what you'd like to do.
Try this:
SELECT T1.col1, t2.col2 FROM Table1 t1 INNER JOIN
(SELECT col1, col2, col3 FROM Table 2) t2 ON t1.col1 = t2.col1

SQL command for conditional join for PROGRESS database

Please bear with me new to SQL- I am trying to write an SQL command with a join in a PROGRESS db. I would like to then select only the first matching record from the join. I thought to use LIMIT but PROGRESS does not support that. MIN or TOP would also work I think but having trouble with the syntax.
Something like this?-
SELECT table1.field 1, table2.field 2
FROM table2
INNER JOIN table2
ON table1.field3=table2.field3
WHERE table1.field4 in (SELECT min(table1.field4) FROM table1)
BUt it appears I can't use MIN there as saying can't do an aggregate there.
Any help would be huge.
try:
SELECT
t1.field1, t2.field2
FROM table1 t1
INNER JOIN table2 t2 ON t1.field3=t2.field3
WHERE t1.field4=(SELECT min(t.field4) FROM table1 t WHERE t1.field4=t.field4)