Writing a query that can be used to find which orders shipped to a different country from the customer - sql

So I am currently in a Database 2 class at my University. We are using the Northwinds db. I haven't used SQL in a few years so I am a little rusty.
I changed a few of the pieces in the Orders table so that instead of 'Germany' it was 'Tahiti'. Now I need to write a query to find which orders shipped where.
I know that I will need to use an Join but I am not exactly sure how. I have gone to W3Schools and looked at the Joins SQL page but still haven't found the correct answer I am looking for.
This is what I have currently (which I am also not sure if it is correct):
SELECT Customers.Country
FROM Customer
WHERE Customer.Country = 'Germany'
INNER JOIN
SELECT Orders.ShipCountry
FROM Orders
WHERE Orders.ShipCountry = 'Tahiti'
So if anyone could give me help I would really appreciate it.
EDIT
So this is the actual question I was given which I think is also kind of poorly worded.
"Suspicious e-commerce transactions include orders placed by a customer in one country that are shipped to another country. In fact there are no such orders in the Northwind db, so create a few by modifying some of the "Germany" shipcountry entries in the orders table to "Tahiti". Then write a query which finds orders shipped to a different country from the customer. Hint: in order to do this, you will need to join the Customers and Orders table."

Is this what you are looking for:
SELECT *
FROM Customer AS C
INNER JOIN
Orders AS O
ON C.CustomerID = O.CustomerID
WHERE C.Country = 'Germany' AND O.ShipCountry = 'Tahiti';
The above query is based on the Schema as defined in CodePlex

i hope that this can help you
SELECT Orders.*
FROM Orders -- table name, also can use alias
inner join Customer -- table name, also can use alias
on Orders.ShipCountry = Customer.Country -- you must declare what is the field to use by join
where Customer.Country in ('Germany','Tahiti')

Related

Need assistance with SQL statement having trouble with the JOINS and WHERE clause

The company is performing an analysis of their inventory. They are considering purging books that are not popular with their customers. To do this they need a list of books that have never been purchased. Write a query using a join that provides this information. Your results should include all the book details and the order number column. Sort your results by the book title.
SELECT o.order_nbr, b.*
FROM orders o JOIN books
WHERE
ORDER BY book_title
This is all I could come up with, I'm still learning Joins and struggling to figure out what the correct statement should be. Wasn't sure what to put in the WHERE clause and don't really know how to properly join these tables.
You need an ON clause to specify what you are joining on. Also, your WHERE clause is empty, and you are not specifying the type of JOIN you are using. Looking at the way the tables are set up, the expectation is you are going to join the BOOKS table on ORDER_ITEMS, which also contains ORDER_NBR.
In the question, it's asking to find books with no orders, so correct join would be a LEFT JOIN between BOOKS and ORDER_ITEMS, as that will include every book, even those without orders, which will have an ORDER_NBR of NULL
The SQL would look like
SELECT o.order_nbr, b.*
FROM books b
LEFT JOIN order_items o on b.book_id = o.book_id
WHERE o.order_nbr is null
ORDER BY book_title
This would return only the books with no orders.

How do I use the . properly with sql?

I am new to sql and have a question about joining 2 tables. Why is there a . in between customers.custnum in this example. What is its significance and what does it do?
Ex.
Select
customers.custnum, state, qty
From
customers
Inner join
sales On customers.custnum = sales.custnum
The . is to specify a column of a table.
Let's use your customer table; we could do:
SELECT c.custnum, c.state, c.qty FROM customers as c INNER JOIN
sales as s ON c.custnum = s.custnum
You don't really need the . unless two tables have columns with the same name.
In the below query, there are two tables being referred. One is CUSTOMERS another is STATE. Since both has same column CUSTNUM, we need a way to tell the database which CUSTNUM are we referring to. Same as there may be many Bob's, if so their last name is used for disambiguation.
I would consider the below style as more clearer. That's opinionated.
Select
cust.custnum, cust.state, s.qty
From
customers cust -- use alias for meaningful referencing, you may be self-joining, during that time you can use cust1, cust2 as aliases.
Inner join
sales as s On cust.custnum = s.custnum
Think of it as a way to categorize the hierarchical nature of the database. Within a DB, there are tables, and within tables there are columns. It's just a way of keeping track, especially if you are working with multiple tables that may have the same column name.
For example, a table called Sales and a table called Customers might both have a column called Date. You may be writing a query where you only want the date from the Sales table, so you would specify that by writing:
Select *
From Sales
inner join Customers on Sales.ID = Customers.ID
where Sales.Date = '1/1/2019'

SQL-92 Selecting with the dot operator

Before it is marked as a duplicate, I am not asking If I have to specify it fully, I am why it does not matter if it is specified. Hope that clears that up. Now to the question.
I'm new to SQL so I'm not sure if there is some technical term for this.
Say I have a database with tables: Orders and Customers.
Orders has categories: OrderID, CustomerID, and OrderDate
Customers has categories: CustomerID, CustomerName, ContactName, and Country
I then have a SQL Query:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
So I am selecting Orders.OrderID, Customers.CustomerName, and Orders.OrderDate FROM Orders table. If it is from the Orders table, why specify Orders. before the OrderID and OrderDate in select ? This is an example from a website, and does not explain this. I am not sure if it has to do with join (which is in the example) so that's why I also put it there and in the tags.
-Thanks
Sometimes the column name is found in both tables and your DBMS will throw an error that the column is ambiguous. It's usually a good idea to explicitly declare which table you want the item to come from.
Using an alias often make the code easier to read and write:
SELECT ord.OrderID, cus.CustomerName, ord.OrderDate
FROM Orders ord
INNER JOIN Customers cus ON ord.CustomerID=cus.CustomerID;
These table names are pretty short, but you can see how useful aliases can be when the table names become longer and more complicated.
One benefit to explicitly declaring the table is that you can tell at a glance what table the data is coming from. Once you have data coming from many sources, through joins or not, it can be difficult to tell exactly which table a field is coming from, if you do not show the table in the select statements.

Why is my WHERE clause not return the CustomerIDs that are also in the Orders table?

My SQL statements are not returning any results. I am using the table that are on the www.w3schools.com web site. I want to have all of the customer IDS in my Customers table match all of the CustomerIDS in the Orders table. The SQL statement works that it goes through the table and checks every CustomerID to every Orders.CustomersID and when it finds a match doesn't it return that record.
Question: Why does the SQL not return the row when both customerIDS are equal because there are values that will return true?
SQL Statement:
SELECT * FROM Customers WHERE Customers.CustomersID = Orders.CustomersID;
One last thought: What is the best free SQL database that can be downloaded without a lot of hassle for home use?
Try this:
SELECT Customers.*
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
If you just want customers that have orders then use:
SELECT *
FROM Customers
WHERE CustomerID IN
(SELECT CustomerID FROM Orders)
If you use a JOIN you may get multiple records per customer (if the customer has multiple orders). You can solve that with DISTINCT but IMHO a IN clause is cleaner (and likely faster)
You need a query like this: SELECT *
FROM Customers c
INNER JOIN Orders o on c.CustomerID = o.CustomerID;
w3schools is a very bad resource.
Selecting the best database is a bad question for stackoverflow. But the answer is PostgreSQL. (And lots of other DBMSs would fit the bill for you as well.)

Select based on the number of appearances of an id in another table

I have a table B with cids and cities. I also have a table C that has these cids with extra information. I want to list all the cids in table C that are associated with ALL appearances of a given city in Table B.
My current solution relies on counting the number of times the given city appears in Table B and selecting only the cids that appear that many times. I don't know all the SQL syntax yet, but is there a way to select for this kind of pattern?
My current solution:
SELECT Agents.aid
FROM Agents, Customers, Orders
WHERE (Customers.city='Duluth')
AND (Agents.aid = Orders.aid)
AND (Customers.cid = Orders.cid)
GROUP BY Agents.aid
HAVING count(Agents.aid) > 1
It only works because I know right now with the HAVING statement.
Thanks for the help. I wasn't sure how to google this problem, since it's pretty specific.
EDIT: I'm pinpointing my problem a bit. I need to know how to determine if EVERY row in a table has a certain value for a field. Declaring a variable and counting the rows in a sub-selection and filtering out my results by IDs that appear that many times works, but It's really ugly.
There HAS to be a way to do this without explicitly count()ing rows. I hope.
Not an answer to your question, but a general improvement.
I'd recommend using JOIN syntax to join your tables together.
This would change your query to be:
SELECT Agents.aid
FROM Agents
INNER JOIN Orders
ON Agents.aid = Orders.aid
INNER JOIN Customers
ON Customers.cid = Orders.cid
WHERE Customers.city='Duluth'
GROUP BY Agents.aid
HAVING count(Agents.aid) > 1
What variant of SQL are you using?
To start with, you can (and should) use JOIN instead of doing it in the WHERE clause, e.g.,
select Agents.aid
from Agents
join Orders on Agents.aid = Orders.aid
join Customers on Customers.cid = Orders.cid
where Customers.city = 'Duluth'
group by Agents.aid
having count(Agents.aid) > 1
After that, I'm afraid I might be a little lost. Using the table names in your example query, what (in English, not pseudocode) are you trying to retrieve? For example, I think your sample query is retrieving the PK for all Agents that have been involved in at least 2 Orders involving Customers in Duluth.
Also, some table definitions for Agents, Orders, and Customers might help (then again, they might be irrelevant).
I'm not sure if I understood you problem, but I think the following query is what you want:
SELECT *
FROM customers b
INNER JOIN orders c USING (cid)
WHERE b.city = 'Duluth'
AND NOT EXISTS (SELECT 1
FROM customers b2
WHERE b2.city = b.city
AND b2.cid <> cid);
Probably you will need some indexes on these columns.