What is the difference between JOIN and simple SELECT in MySQL? - mysql

I have two mySQL statements. First is:
SELECT o.OrderID, c.CustomerName, o.OrderDate
FROM Customers AS c, Orders AS o
WHERE c.CustomerID=o.CustomerID;
The second is:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
Both produce the same result, but second doesn't contain reference on Customers table in FROM request.
My question is - what is the difference between these two sql statements? In which cases should I use JOIN and in which cases should I use simple SELECT from two tables?

They are the same except the second is easier to read, so you should use that one.

Those JOIN are different, although the result are the same.
The First one is CROSS JOIN and adds the condition in where, which is implicit CROSS JOIN
The second one is INNER JOIN
If you want to connect two tables I would use INNER JOIN instead of CROSS JOIN Because the intention of the inner join table is clearer

Related

ON clause in SQL join-does order matter?

Are these two same?
- SELECT p.name,o.order_time FROM ORDERS o INNER JOIN PRODUCTS p on **o.product_id=p.id**;
- SELECT p.name,o.order_time FROM ORDERS o INNER JOIN PRODUCTS p on **p.id=o.product_id**;
And are these two the same thing:
- SELECT p.name,o.order_time FROM ORDERS o LEFT JOIN PRODUCTS p on **o.product_id=p.id;**
- SELECT p.name,o.order_time FROM ORDERS o LEFT JOIN PRODUCTS p on **p.id=o.product_id;**
Yes, first 2 queries are exactly same in terms of performance and result. In addition to that, first 2 queries is same as below query,
SELECT p.name,o.order_time FROM PRODUCTS p INNER JOIN ORDERS o on p.id=o.product_id;
Which in turn is same as,
SELECT p.name,o.order_time FROM PRODUCTS p INNER JOIN ORDERS o on o.product_id=p.id;
Note: the order of inner join tables is swapped as well.
3rd and 4th are same as well in terms of performance and result. You can't swap the order of table here, unless dataset guarantees same result.
Based on the dataset, first 2 queries can yield same or different result than last 2 queries.
Reason is = is commutative, it means a=b is same as b=a. Extending it further, just INNER JOIN is also commutative.

is it possible to UNION and INNER JOIN for sql

im using the w3schools sql database to learn and i have come across a question.
is it possible to sort the results according to CustomerName after i have done a INNER JOIN?
this is the INNER JOIN statement
SELECT Orders.OrderID, Customers.CustomerName, Customers.ContactName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
The UNION command in SQL is used to combine the results of your first query with the entire results of a second query that has the same columns. It is not used for ordering the first query. With your INNER JOIN you can sort the results just by adding ORDER BY:
SELECT Orders.OrderID, Customers.CustomerName, Customers.ContactName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
ORDER BY Customers.CustomerName;

SQL ambigutiy when trying to display

Consider the following query i tried, there two tables, Orders and Customers, each have column name CustomerID, when i try to display both CustomerID's only one column is displaying, i can't understand why is it so, or am i understanding the basics wrong.
SELECT Customers.CustomerID,Orders.CustomerID
FROM customers
inner JOIN orders
on customers.customerid=orders.customerid;
When i try to display only one column it is displaying well and good
SELECT Customers.CustomerID
FROM customers
inner JOIN orders
on customers.customerid=orders.customerid;
and
SELECT Customers.CustomerID
FROM customers
inner JOIN orders
on customers.customerid=orders.customerid;
So my problem is why I cant display the both.
use alias
SELECT Customers.CustomerID as customerid,Orders.CustomerID as ocustomerid
FROM customers
inner JOIN orders
on customers.customerid=orders.customerid;
You need to define an alias for each column:
SELECT Customers.CustomerID as customer_id, Orders.CustomerID as order_id
FROM customers
inner JOIN orders
on customers.customerid=orders.customerid;
This is a peculiarity of some query interfaces. Your first query (which I would write like this) is:
SELECT c.CustomerID, o.CustomerID
FROM customers c INNER JOIN
orders o
ON c.customerid = o.customerid;
This returns two columns, both named CustomerId. Some query interfaces insist that the resulting columns be unique in the result set. Hence, the results ignore "subsequent" columns with the same name.
You can get a flavor of this by using the query as a subquery:
SELECT x.*
FROM (SELECT c.CustomerID, o.CustomerID
FROM customers c INNER JOIN
orders o
ON c.customerid = o.customerid
) x;
This should return an error, because CustomerId is ill-defined.
Three points to remember:
Most databases and query interfaces allow result sets with multiple columns with the same name.
No database allows multiple columns with the same name in a subquery.
You know how to fix this by assigning a column alias, which is a best-practice anyway.

Inner Join and Cross Join yielding the same result

use classicmodels;
select Orders.OrderNumber,
Customers.CustomerName, Orders.Status, orders.shippeddate,
Customers.Country
from Customers **cross join/inner join** Orders
on Customers.CustomerNumber = Orders.customerNumber
order by 1 asc
Hi all, I'm really confused as to why inner join in my query isn't really any different from the result of the cross join? I thought that cross join would result of a Cartesian product but both joins are giving me 326 rows. I've also seen somewhere that I shouldn't use non-unique data?
From the MySQL JOIN docs:
In MySQL, JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents
(they can replace each other). In standard SQL, they are not
equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used
otherwise.

Joining two tables unsure of join to use

I have to get data from two SQL tables but I am unsure of what join to use and the same tables have the same customers_id table would I use an inner join?
table - customers
customers_id
customers_firstname
customers_lastname
customers_email_address
table - orders
customers_id
customers_street_address
customers_suburb
customers_city
customers_postcode
customers_state
customers_country
It depends on what results you want. If you want every record in Customers regardless of whether there is a matching record in orders the you would use an LEFT OUTER JOIN. If not you would use an INNER JOIN. This is the article I typically refer people to for a basic explanation of joins.
An INNER JOIN would look like this:
SELECT c.customers_id,
c.customers_firstname,
c.customers_lastname,
o.customers_street_address,
o.customers_city,
o.customers_postcode,
o.customers_state
FROM customers as c
INNER JOIN orders as o
ON c.customers_id = o.customers_id
I purposely did not do select *. Try to get into the habit of only selecting the columns you want from tables instead of everything.
The join you should use depends on which information you are trying to retain, based on the customer ID. If you're looking to retain all of the information from customers even if there is no match with orders you would use a left join. Conversely, if you wanted to retain all the information from orders even if there were no match with customers you would use a right join.
If you want to return all of the information, and combine rows which have a match, use a full join. Finally, you can use an inner join if you only want the rows which have a match from either table.
the same tables have the same customers_id table
I think you meant to say they have a common column, being customers_id, in which case you can use NATURAL JOIN.
INNER JOIN Ya you can use it.
You can write below query to get your all data from two tables.I am going to write full join query for you.y
select customers.customers_firstname,
customers.customer_lastname,
orders.customers_city
from customers
INNER JOIN orders
ON customer.customers_id = orders.customers_id