SQLITE INNERJOIN Nightmare, need a solution to this - sql

It's nice to find such a useful site with genius members. I have been trying to find a solution for this SQLITE problem for a while now. Google didn't help me, except in finding this website. The SQL query works fine on the MSAccess version of the same database.
Here's my SQL statement - which didn't work for me.
SELECT Invoices.InvoiceNumber, Invoices.Quantity,Invoices.Code, Invoices.Price,Invoices.Discount, Invoices.InvoiceGrandTotal, Employees.EmployeeName, Customers.CustomerName, Invoices.DateOfInvoice, [price]*[Quantity] AS Total, Customers.Address, Products.Description,Products.Unit
FROM Products
INNER JOIN (
(
( Invoices INNER JOIN InvoiceDetails
ON Invoices.InvoiceNumber = InvoiceDetails.InvoiceNumber
) INNER JOIN Customers
ON Invoices.CustomerID = Customers.CustomerID
) INNER JOIN Employees
ON Invoices.UserID = Employees.EmployeeID
) ON Products.Code = InvoiceDetails.Code
WHERE (((InvoiceDetails.InvoiceNumber)='10111'));
The error message is: "Cannot compile Select-Statement: no such column: Invoices.InvoiceNumber"

That usually just means that you mis-spelled the column name ... check your Invoices table and make sure the column is InvoiceNumber and not "Invoice_Number" or something similar ...
Also, a much simpler version of this query would look something like this .. without all the strange nesting:
SELECT
Invoices.InvoiceNumber,
Invoices.Quantity,
Invoices.Code,
Invoices.Price,
Invoices.Discount,
Invoices.InvoiceGrandTotal,
Employees.EmployeeName,
Customers.CustomerName,
Invoices.DateOfInvoice,
[price]*[Quantity] AS Total,
Customers.Address,
Products.Description,
Products.Unit
FROM
Invoices
JOIN Employees
ON Employees.EmployeeID = Invoices.UserID
JOIN Customers
ON Customers.CustomerID = Invoices.CustomerID
JOIN InvoiceDetails
ON InvoiceDetails.InvoiceNumber = Invoices.InvoiceNumber
JOIN Products
ON Products.Code = InvoiceDetails.Code
WHERE
InvoiceDetails.InvoiceNumber = '10111'

I think the issue might be with case sensitivity. Unless I'm mistaken, MS Access field names are not case sensitive. Check the offending column name for the correct casing in you SQLITE table definition.

Related

SQL query with having clause returns unexpected results

I am working with Chinook database and would like to write a query that returns genres most sold in USA. My idea uses having clause and returns table which is wrong.
I've already written something that works that doesn't use having clause however I would like to understand why my idea with having clause does not work. :)
Here's my SQL query:
select g.name, count(il.invoice_line_id) tracks_sold from genre g
inner join track t
on t.genre_id = g.genre_id
inner join invoice_line il
on il.track_id = t.track_id
inner join invoice i
on i.invoice_id = il.invoice_id
inner join customer c
on c.customer_id = i.customer_id
group by 1
having c.country = 'USA'
order by tracks_sold desc
It returns following result:
It is wrong. It should return instead below table:
When I write the query by first separating tracks sold in USA and then joining and grouping by genre it works fine. I wonder why having clause does not work here as I expected.
Below you can find outlook of the database with columns that I use:
Many thanks in advance!

Still can't delete in SQL using JOIN

I am using PostgreSQL. I saw other posts with people having trouble using join while deleting, which said to specify the table you are deleting from. I did that, and this is still not working. It says there is a "syntax error at or near 'Customers'. I am using the famous Northwind database.
DELETE Customers FROM Customers INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
WHERE Customers.CustomerID = 5;
The code doesn't really do anything useful, I just want to know why I can't delete using JOIN.
As documented in the manual you can't use JOIN like that in a DELETE statement. To join tables to the base table you need to use the USING clause:
DELETE FROM Customers
using Orders
where Customers.CustomerID=Orders.CustomerID
and Customers.CustomerID = 5;
This is equivalent to the SQL without a join:
delete from customers as c
where c.customerid = 5
and exists (select *
from orders o
where o.customerid = c.customerid);
A join in a delete will never work. If you want to ensure that the customer has an order, you could try something like this:
DELETE FROM Customers
WHERE
CustomerID = 5 AND
CustomerID IN (SELECT o.CustomerID FROM Orders o);

Query with columns from 4 tables in SQL

Can anyone who knows SQL, specifically the flavor used in Microsoft Access 2013, tell me what I'm doing wrong here?
SELECT custid, custname, ordno, itemno, itemname
FROM cust
INNER JOIN order
ON cust.custid = order.custid
INNER JOIN orderitems
ON order.ordno = orderitems.ordno
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
I've already read other, similar questions, and tried the methods they used in their solutions, but I'm getting a "Syntax error in FROM clause.", almost no matter what I try.
* * *
SOLUTION: Thanks for the replies! In addition to adding square brackets around "order" and using TableName.ColumnName syntax in SELECT, I had to use parentheses for my multiple INNER JOINs. Here is the fixed code:
SELECT cust.custid, cust.custname, [order].ordno, orderitems.itemno, inv.itemname
FROM ((cust
INNER JOIN [order]
ON cust.custid = [order].custid)
INNER JOIN orderitems
ON [order].ordno = orderitems.ordno)
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
SELECT cust.custid --<-- Use two part name here
,cust.custname
,[order].ordno
,orderitems.itemno --<-- Only guessing here use the correct table name
,inv.itemname --<-- Only guessing here use the correct table name
FROM cust
INNER JOIN [order]
ON cust.custid = [order].custid --<-- used square brackets [] around ORDER as it is
INNER JOIN orderitems -- a key word.
ON [order].ordno = orderitems.ordno
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
In your Select Statament you need to use Two Part name i.e TableName.ColumnName since these column can exist in more than one Tables in your FROM clause you need to tell sql server that columns in your select coming from which table in your from clause.

What is wrong with my join in this query?

Im practicing basic SQL with this site http://www.sqlishard.com/Exercise
Here is the question:
S5.0 - INNER JOIN
Now that we can pull data out of a single table and qualify column
names, let's take it a step further. JOIN statements allow us to
'join' the rows of several tables together using a condition to define
how they match one another. SELECT [columns] FROM FirstTable INNER
JOIN SecondTable ON FirstTable.Id = SecondTable.FirstTableId
Try using the INNER JOIN syntax to SELECT all columns from the
Customers and Orders tables where the CustomerId column in Orders
matches the Id column in Customers. Since both tables have an Id
column, you will need to qualify the Customers id in the WHERE clause
with either the table name or a table alias.
Here is my answer:
SELECT *
FROM Customers AS c
INNER JOIN Orders AS o ON c.ID = o.ID
WHERE o.CustomerID = c.ID
The site says im wrong? Could anyone explain where i'm going wrong?
EDIT: I see now I dont need the WHERE clause, but the question states..
you will need to qualify the Customers id in the WHERE clause with
either the table name or a table alias.
Hence my confusion. Thanks none the less.
Try this way:
SELECT c.ID,o.ID
FROM Customers AS c
INNER JOIN Orders AS o ON o.CustomerID = c.ID
or using where clause
SELECT *
FROM Customers AS c, Orders AS o
where o.CustomerID = c.ID
If you use JOIN.. ON, you do not need where clause

Subquery automatically producing cross join

I am not certain WHY but when I follow the example (from the northwind datase in a ms sql server) to do a subquery on Microsoft SQL Server Management Studio 2008 by typing in the code like shown below,
Select Orders.OrderID,
(Select Customers.CompanyName
From Customers
Where Customers.CustomerID = Orders.CustomerID) As Company Name
From Orders,
Customers
This sql code with subquery automatically gained a cross join and become
Select Orders.OrderID,
(Select Customers.CompanyName
From Customers
Where Customers.CustomerID = Orders.CustomerID) As Company Name
From Orders
CROSS JOIN Customers as Customers_1
I have played around with several variation of this but with no luck in eliminating this problem. Is this a known bug for microsoft sql server management studio 2008? If so, has it been patched, how do I find the patched? Otherwise, how can I report this to Microsoft and get them to really fixed it quickly?
In my actual query, I need to query/lookup the name of this particular table about 50 times by equating the ID and I think it is simply dumb having to do a JOIN of any sort for this because the code is crumpy, VERY long, and performance may be poor?
The subquery isn't causing the cross join, the lack of a condition controlling the join is. You need something like this:
Select Orders.OrderID, (Select Customers.CompanyName From Customers Where Customers.CustomerID = Orders.CustomerID) As Company Name
From Orders, Customers
Where Orders.CustomerID = Customers.CustomerID
I don't know why a sub-query is suggested by your book -- I would do it like this:
Select Orders.OrderID, Customers.CompanyName
From Orders
left join Customers on Customers.CustomerID = Orders.CustomerID
Looks like it should be a correlated-subquery
Select Orders.OrderID,
(Select Customers.CompanyName
From Customers
Where **Customers.CustomerID = Orders.CustomerID**) As Company Name
From Orders
--,
-- Customers
Why would you need Customers again when the inner Correlated Subquery brings the customer Name for each Order that is processed?
The Management Studio's insistence on adding CROSS JOIN is a warning that you are doing something strange. Trying to query two tables: Customer,Orders without any join condition.
Also, the query optimizer will usually convert these correlated sub-queries into joins during processing, but you can use the clearer syntax where appropriate.
Where is it appropriate? Particularly if you need to generate some sort of aggregate on the inner query.