Can we select data from 2 tables in same time - sql

From the code as shown below. I wonder that why it can select data from 2 tables in same time that are table "venue AS v" and table "as s".
or I misunderstand?
SELECT name, s.num_match
FROM venue AS v,
(SELECT venue_id, COUNT(*) AS num_match
FROM match
GROUP BY venue_id) AS s
WHERE v.venue_id = s.venue_id
LIMIT 3;

Yes you can using JOIN clause for example.
More infos here: https://www.informit.com/articles/article.aspx?p=30875&seqNum=5

Yes you can select data from as many as tables you want at the same time.
In this case you are trying to get an aggregated number from table-s and join it with the table v.
There are many ways to write the code to join the table. Above is one method which you have used.

Related

Join multiple columns from 2 tables in a SQL database

This is on a Postgres server using SQL. I have a supply_chain table in which I need to pull the warehouse_id from, and an order table in which I need to pull the total returns. Located on the same server.
I need to combine them on the delivery zipcode from the order table and the zipcode on the supply_chain table. I am unsure the best way to join this in SQL.
SELECT deliveryzipcode, COUNT(OrderReturned) AS Total_returned
FROM transactions_log
WHERE OrderReturned= 'Yes'
GROUP BY deliveryzipcode;
This query will successfully return the number of returns based on zipcode. So basically I need to pull those warehouse_id's and count them.
Apologize in advance for not wording this question well.
You can try this :
SELECT sc.warehouse_id, sc.zipcode, tl.Total_returned
FROM logistics_supply_chain_network AS sc
INNER JOIN
(
SELECT deliveryzipcode, COUNT(OrderReturned) AS Total_returned
FROM transactions_log
WHERE OrderReturned= 'Yes'
GROUP BY deliveryzipcode
) AS tl
ON tl.deliveryzipcode = sc.zipcode ;

Add / update column from a query SELECT? SQL

I'm quite a novice on this and I don't know if I will explain myself well. I am trying to do an exercise in SQL in which asks me to update the data in an "X" table from other data in a "Y" table. The problem is that it is not about updating table X exactly like the data in table Y. I put the statement and my tables:
Update the "numJocs" field (number of games) for all platforms, depending on the number of games each of the platforms in the GAMES table has.
PLATFORM table:
where: "nom" is name.
GAMES table:
where: "nom" is name, "preu" is price, "idPlataforma" is idPlatform and "codiTenda" is storeCode, but only idPlataforma interested for this exercise.
If I do:
SELECT COUNT(games.idPlataforma)
FROM games
GROUP BY (games.idPlataforma)
I can see how many games there are for each platform. The result would be:
count(games.idPlataforma)
__________________________
2
1
2
2
I would like to be able to put this result in the PLATFORM table, column "numJocs". But I don't know how to do it ... I also don't want to put it manually, that is, a "2" in a row "1", etc ... but I would like to be able to make a query and add that query in the column that I have to fill in. He tried to do a thousand things, but nothing ... Any help?
Thanks!!
for one time update you can use below query
update Product P
INNER JOIN (
SELECT games.idPlataforma, COUNT(games.idPlataforma) as cnt
FROM games
GROUP BY games.idPlataforma
) x ON P.id= x.idPlataforma
SET P.numJocs= x.cnt
For the next time on every entry of new game you have a update numJocs
Suppose you have 2 tables, table 1 and table 2:
Table 1:
Table 2:
You could insert new values into table 1, based on table 2 by doing the following:
insert into Table1(number,CFG) select ITEM,results from Table2
Which has the following result in table 1:
Any database should support the syntax using a correlated subquery:
update platforms
set numjocs = (select count(*)
from games g
where g.idPlatforms = platforms.id
);
I would caution you though about storing this value in the table. It will be immediately out of data if the platforms table changes. If you want to keep it in synch, then you need to create triggers -- and this is all rather complicated.
Rather, calculate the data on the fly:
select p.*,
(select count(*)
from games g
where g.idPlatforms = platforms.id
) as numjocs
from platforms p ;
You can put this in a view if you like. Many databases support materialized views where the results of the view are stored in a "table" and the table is kept up-to-date with the underlying data.

SQL SELECT FOR SALES WITH 'COUNT' FROM ANOTHER TABLE

I have two tables. The first one is for sales (name's table is 'ventas') and the other one, for detailed articles by sale (the name is 'ventaArticulos'). Basically, the last one contains all the articles that were sold.
Those are related by the columns ventas.id_venta and ventaArticulos.id_ventaArticulo
Basically, the idea is to make an SQL SELECT for the first table (ventas) for example, getting the columns 'fecha' and 'importe' but also, perform a 'count' with the total of registers that are in the second table related by sale. (ventas.id_venta and ventaArticulos.id_ventaArticulo)
hope to be clear enough and can help me!
SQL to try to clarify (Obviously it doesn't work):
SELECT ventas.fecha, ventas.importe, count(ventaArticulos.id_codigoArt)
FROM ventas JOIN
ventaArticulos
ON ventaArticulos.id_ventaArticulo = ventas.id_venta
Thanks!
I would recommend to use table alise that could be easier to follow & you forgot to include GROUP BY Clause
SELECT v.fecha, v.importe, count(va.id_codigoArt) counts
FROM ventas v -- Use alise v entire the query instead of table_name
INNER JOIN ventaArticulos va ON va.id_ventaArticulo = v.id_venta
GROUP BY v.fecha, v.importe;
SELECT v1.fecha, v1.importe, count(v2.id_codigoArt)
FROM ventas v1 , ventaArticulos v2
where v1.id_ventaArticulo= v2.id_venta
group by v1.fecha, v1.importe
having count(*) > 1

SQL Query to fetch information based on one or more condition. Getting combinations instead of exact number

I have two tables. Table 1 has about 750,000 rows and table 2 has 4 million rows. Table two has an extra ID field in which I am interested, so I want to write a query that will check if the 750,000 table 1 records exist in table 2. For all those rows in table 1 that exist in table 2, I want the respective ID based on same SSN. I tried the following query:
SELECT distinct b.UID, a.*
FROM [Analysis].[dbo].[Table1] A, [Proteus_8_2].dbo.Table2 B
where a.ssn = b.ssn
Instead of getting 750,000 rows in the output, I am getting 5.4 million records. Where am i going wrong?
Please help?
You're requesting all the rows in your select if b.UID is a unique field in column two.
Also if SSN is not unique in table one you can get the higher row count than the total row count for table 2.
You need to consider what you want from table 2 again.
EDIT
You can try this to return distinct combinations of ssn and uid when ssn is found in table 2 provided that ssn and uid have a cardinality of 1:1, i.e., every unique ssn has a single unique uid.
select distinct
a.ssn,b.[UID]
from [Analysis].[dbo].[Table1] a
cross apply
( select top 1 [uid] from [Proteus_8_2].[dbo].[Table2] where ssn = a.ssn ) b
where b.[UID] is not null
Try with LEFT JOIN
SELECT distinct b.UID, a.*
FROM [Analysis].[dbo].[Table1] A LEFT JOIN [Proteus_8_2].dbo.Table2 B
on a.ssn = b.ssn
Since the order detail table is in a one-many relationship to the order table, that is the expected result of any join. If you want something different, you need to define for us the business rule that will tell us how to select only one record from the Order detail table. You cannot effectively write SQL code without understanding the business rules that of what you are trying to achieve. You should never just willy nilly select one record out of the many, you need to understand which one you want.

select * with column(s) from different table merging with that of original table's columns

These are my tables(with some records):
Product:
ID PRICE NAME DESCRIPTION
1 100 laptop laptop_desc
2 200 mouse mouse_desc
3 300 伊吾 伊吾伊吾
Product_Translations:
PID LANG NAME DESCRIPTION
1 ch 伊吾 伊吾伊吾
And using the following query I could JOIN the 2 tables:
SELECT p.id,p.price,
COALESCE(pt.Name, p.Name) Name,
COALESCE(pt.Description, p.Description) Description
FROM Product p
LEFT JOIN Product_T pt
ON pt.PID = p.ID
AND pt.LANG = 'ch'
WHERE (UPPER(p.name) LIKE UPPER('%伊吾%') OR UPPER(pt.name) LIKE UPPER('%伊吾%'))
Now my question is that in the above query I need to mention each and every column in the select query explicitly(like p.id, p.price; real table has lot more columns) which I don't want to do. In my case I need to get all columns all the time, i.e, select *. But If give p.* in the query, name and description columns are coming twice which is expected(from Product and Product_T tables). So is there any way I could still give p.* and get the merged name and description columns ?
Thanks for your help.
You could query the schema to find the columns on your tables and then dynamically build your select statement from that. (E.g. if a column with the same name appears in both tables only select it from one of them).
See http://hsqldb.org/doc/2.0/guide/databaseobjects-chapt.html#dbc_information_schema for information on how to query the schema in hsqldb.