Simple SQL Script in W3Schools not working - sql

I am currently learning SQL and using the W3Schools Tryit Editor to play around. I am trying to update a table using a lookup from another table. I looked online and figured out the following code to run:
UPDATE OrderDetails
SET OrderDetails.ProductID = Orders.CustomerID
FROM Orders
INNER JOIN OrderDetails
ON OrderDetails.OrderID = Orders.OrderID
But it is coming up with the following error: "Error 1: could not prepare statement (1 near ".": syntax error)"
Is there a problem with my code or are there things that W3Schools doesn't want to run?

The update with join syntax is specific for each db
The query you are using is valid for SQLSERVER
UPDATE OrderDetails
SET OrderDetails.ProductID = Orders.CustomerID
FROM Orders
INNER JOIN OrderDetails
ON OrderDetails.OrderID = Orders.OrderID
but not for my MySQL
UPDATE OrderDetails
INNER JOIN OrderDetails
ON OrderDetails.OrderID = Orders.OrderID
SET OrderDetails.ProductID = Orders.CustomerID

This works:
UPDATE OrderDetails
SET ProductID = (SELECT CustomerID FROM Orders WHERE OrderDetails.OrderID = Orders.OrderID)
WHERE OrderID IN (SELECT OrderID FROM Orders);
Is this what you want?

Related

SQL - How can you use WHERE instead of LEFT/RIGHT JOIN?

since I am a bit rusty, I was practicing SQL on this link and was trying to replace the LEFT JOIN completly with WHERE. How can i do this so it does the same thing as the premade function in the website?
What I tried so far is:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers, Orders
WHERE Customers.CustomerID = Orders.CustomerID OR Customers.CustomerID != Orders.CustomerID
Order by Customers.CustomerName;
Thanks in advance for your help.
You are trying to replace
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
with
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers, Orders
WHERE ???
this is doomed to failure. Consider Customers has two rows and Orders has zero. The outer join will return two rows.
The cross join (FROM Customers, Orders) will return zero rows.
In standard SQL a WHERE clause can only reduce the rows from that - not increase them so there is nothing you can put for ??? that will give your desired results.
Before ANSI-92 joins were introduced some systems used to have proprietary operators for this, such as *= in SQL Server but this was removed from the product.
This may work for you.
SELECT
c.CustomerName,
o.OrderID
FROM Customers c
LEFT JOIN Orders o
on c.CustomerID = o.CustomerID
Order by c.CustomerName;
If you are trying to replace this:
SELECT c.CustomerName, o.OrderID
FROM Customers c LEFT JOIN
Orders o
ON c.CustomerID = o.CustomerID
ORDER BY c.CustomerName;
Then you can use UNION ALL:
SELECT c.CustomerName, o.OrderID
FROM Customers c JOIN
Orders o
ON c.CustomerID = o.CustomerID
UNION ALL
SELECT c.CustomerName, o.OrderID
FROM Customers c
WHERE NOT EXIST (SELECT 1 FROM Orders o WHERE c.CustomerID = o.CustomerID)
ORDER BY CustomerName
However, the LEFT JOIN is really a much better way to go.

Query Issues! Assistance requested

Not sure what is wrong with my query, but I can't get it to return any results. I'm using the 2012 version of Northwind as far as I know, and I am trying to get the ProductID, ProductName, Supplier Name, and Quantity purchased for each customer, whose value I am retrieving from a DropDownList as the parameter p1.
My query is as follows:
SELECT Products.ProductID,
Products.ProductName,
Suppliers.CompanyName,
[Order Details].Quantity
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID
INNER JOIN Products ON [Order Details].ProductID = Products.ProductID
INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
WHERE (Orders.CustomerID = #p1);
If anyone can figure out the issue, I would greatly appreciate it.
When i run your query without the parameter the result is just fine, so we're safe to say there is something wrong with your input.
when i run it like this there is also no problem, didnt you forget something like the DECLARE?
DECLARE #p1 NVARCHAR(30)
SET #p1 = 'VINET'
Cheers!

SQL: How to select two input parameters from three tables?

Okay, this is related to database given on http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
I need to find list of Customers,OrderID, ProductID for orders from Spain.
The table 'Orders' has OrderID and table 'Products' consists of ProductID whereas the table 'OrderDetails' consists of OrderID and ProductID. I use the following code but I get an error message 'Error: 1 ambiguous column: OrderID'
Here is my code
SELECT CustomerName, Country, OrderID, ProductID
FROM Customers, Orders, Products, OrderDetails
WHERE Customers.CustomerID = Orders.CustomerID
AND Orders.OrderID = OrderDetails.OrderID
AND Products.ProductID = OrderDetails.ProductID AND Country = 'Spain'
Can someone correct any mistake?
You need to use the alias names like this:
SELECT Customers.CustomerName,
Customers.Country, --Specify the correct table in which you have Country column
Orders.OrderID,
Products.ProductID
FROM Customers
inner join Orders ON Customers.CustomerID = Orders.CustomerID
inner join OrderDetails on Orders.OrderID = OrderDetails.OrderID
inner join Products on Products.ProductID = OrderDetails.ProductID
where Customers.Country = 'Spain'
Also try to avoid comma seperated JOINS. A good read: Bad habits to kick : using old-style JOINs

Select multiple selected values from a column from each row

I am new to SQL so please bear with me. I am trying to select rows only with specific product codes. When I run it for one selection without OR statement in the below query results are fine but as soon I add another choice with OR statement the results are not accurate. It looks like it keeps putting ProductCode MW-BShorts-0009 against each OrderID.
SELECT
Orders.OrderID, OrderDetails.ProductCode
FROM
Orders, OrderDetails
WHERE
Orders.OrderID = OrderDetails.OrderID
AND OrderDetails.ProductCode = 'MacFRC'
OR OrderDetails.ProductCode = 'MW-BShorts-0009'
Use IN clause and its better to use Join instead of Cartesian product
SELECT Orders.OrderID, OrderDetails.ProductCode
FROM Orders
JOIN OrderDetails
ON Orders.OrderID = OrderDetails.OrderID
AND OrderDetails.ProductCode IN ('MacFRC','MW-BShorts-0009')
From your above query it looks like you dont even need a join or select from the Orders table
select OrderID, ProductCode
from OrderDetails
where ProductCode IN ('MacFRC','MW-BShorts-0009')
AND has precedence over OR so you'd need to enclose the OR clause in parentheses:
SELECT Orders.OrderID, OrderDetails.ProductCode
FROM Orders, OrderDetails
WHERE Orders.OrderID = OrderDetails.OrderID
AND
(OrderDetails.ProductCode = 'MacFRC'
OR OrderDetails.ProductCode = 'MW-BShorts-0009')
However you should use a JOIN clause instead of adding a WHERE condition to match the tables , and can use IN instead of ORing two conditions:
SELECT Orders.OrderID, OrderDetails.ProductCode
FROM Orders
INNER JOIN OrderDetails
ON Orders.OrderID = OrderDetails.OrderID
WHERE OrderDetails.ProductCode IN ('MacFRC', 'MW-BShorts-0009')
You can use IN as Srikanth has mentioned. But you need to understand the concept of operator precedence as well. In your case, you are saying "Either Orders.OrderID must equal OrderDetails.OrderID and OrderDetails.ProductCode must equal 'MacFRC', or OrderDetails.ProductCode must equal 'MW-BShorts-0009'." What you want is for the OrderID = ProductCode criterion to apply to either ProductCode.
All you need to do is override the default precedence with parentheses, same as you would if you want to do an addition before a multiplication. Like this:
SELECT Orders.OrderID, OrderDetails.ProductCode
FROM Orders, OrderDetails
WHERE Orders.OrderID = OrderDetails.OrderID
AND (OrderDetails.ProductCode = 'MacFRC'
OR OrderDetails.ProductCode = 'MW-BShorts-0009')

Writing sql queries from multiple tables

I could use some help writing a SQL query. I'm trying to display some data from one table but the data I need depends on a value from a different table. I'm pretty new to this so I will try to explain this the best I can:
I have an Orders table with a ShipCity and OrderId columns. I would like to get the OrderId value from Orders where ShipCity = Caracas. using those distinct OrderId values, I would like to query a different table called Order Details where [Order Details].[OrderId] = [Orders].[OrderId] (= to 'Caracas').
I hope that made sense. where I'm getting stuck is I'm sure that I will either need to create some variables or a temporary table to store these values and I don't have any experience with these things yet. I'd appreciate any help. also, these are tables in the Northwind sample database if that helps. below is a dummy sample of what I'm trying to do.
Select OrderId
FROM [Orders]
WHERE ShipCity = 'Caracas'
Select OrderId
FROM [Order Details]
WHERE OrderId = (Orders.ShipCity = 'Caracas')
here's another way of looking at it:
SELECT OrderId
FROM [Order Details]
WHERE OrderId = [Orders].ShipCity = 'Caracas'
You want to use an INNER JOIN
SELECT [Order Details].*
FROM [Order Details]
INNER JOIN [Orders] ON [Orders].OrderId = [Order Details].OrderId
WHERE [Orders].ShipCity = 'Caracas'
More information about joins can be found in the Wikipedia entry or here.
you use a JOIN clause to combine data from two or more tables. Something like this, although you should double check the syntax
select *
from [Orders] o
join [Order Details] od on o.orderid = od.orderid
where o.shipcity = 'Caracas'
You need to join the two tables:
SELECT DISTINCT o.OrderId
FROM Orders o INNER JOIN [Order Details] od ON o.OrderId = od.OrderId
WHERE o.ShipCity = 'Caracas'
...but why do you need the Order Details table in the query?
How about doing with SubQuery method?
SELECT OrderId
FROM [Order Details]
WHERE (OrderId IN SELECT OrderId FROM Orders WHERE ShipCity = 'Caracas')