SQL-92 Selecting with the dot operator - sql

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.

Related

Student Seeking Advice for a CSC Exam

I'm a student taking a course on SQL and DB. My question is this: how does one get good at hand writing queries? Our final exam will consist of many of these questions, and I want to do well. We aren't allowed any sort of reference sheet either, just fyi.
I suppose what I'm asking is: how would you approach this?
In short, You require practice aka hands on sql.
You will probably get many opinions on this from others. Aside from practice and reading, try to ensure you understand the absolute basics and sequence of query.
Always use table.column or alias.column to help prevent any ambiguity of where something is coming from.
Know the overall basic segments of writing a query such as
select
[all your alias.columns comma separated]
from
[your primary and/or JOIN/LEFT JOIN/etc tables]
where
[what is the criteria you are looking for]
AND [use proper parenthesis to prevent ambiguity if so needed]
group by
[any columns if doing aggregates such as count, min, max, avg, etc]
[you need to list all NON-AGGREGATE alias.columns]
having
[if any, such as count(*) > someValue]
order by
[any specific columns and ascending or descending order]
[such as orderDate DESC to put most recent order at top]
In my opinion, getting your FROM clause is one of the most important and I try to always list my table JOIN clauses on first table/alias = second table/alias. Indentation helps here so you can see how you get from one table to the next. At this point, do not think of your filtering (YET), just HOW the tables are related. Then you can add "AND" criteria for something you are specifically looking for from that source.
An example of orders. Looking for customers who ordered in the last 30 days. Start with that source as your first FROM table, everything else off of that. So I start with the orders because I care about WHEN something was ordered. I can then join to customers to get their name.
select
c.LastName,
c.FirstName,
o.OrderDate
from
Orders o
JOIN Customers c
on o.CustomerID = c.CustomerID
where
o.OrderDate > [sql-specific current date - 30 days]
order by
c.LastName,
c.FirstName
Another example of orders that ordered a specific item in the last 30 days. In this case, I could reverse the order of details as specific things being ordered might be smaller granularity vs everything. So, altering above such as
select
c.LastName,
c.FirstName,
o.OrderDate
from
Items i
JOIN OrderDetails od
on i.ItemID = od.ItemID
JOIN Orders o
on od.OrderID = o.OrderID
AND o.OrderDate > [sql-specific current date - 30 days]
JOIN Customers c
on o.CustomerID = c.CustomerID
where
i.ItemDescription = 'SomeThing'
order by
c.LastName,
c.FirstName
Notice my indentation nesting. Personal style preference, but at least you can see how alias i to od, od to o, o to c. In my preference, easier to see the trail of tables and how each are directly related. I also added the "AND" clause to filter out orders within the last 30 days directly in the JOIN to the orders table.
LEFT JOINs, I do the same and keep the criteria directly at the JOIN level. If you put a criteria of a left-join into the WHERE clause (without explicitly handling NULL OR [condition] it turns a left-join into an [INNER] join.
Hope this basic guidance helps you get more comfortable as you get more into writing your own queries and course/test preparation.

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'

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

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')

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.)

Translate SQL query to absolute beginner (me)

I am trying to understand the JOIN command from w3schools tutorial and there is an example.
Can you please "translate" it to me what exactly it does? I already know what do the dots do, but INNER JOIN, ON and so on messed me up?
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
And does it creates a new table in my SQL database or it just creates (lets call it..) "virtual" table which I can use it in the moment
The dot notation here signifies the column of a table.
Table.Column
So the SELECT is retrieving the columns specified from each table.
JOIN matches the tables based on the condition that appears after ON
this queries joins customers to orders based the condition of having the same CustomerID.
For more on joins check out http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Perhaps the first thing to understand is the "dot notation." When you see a value like Orders.CustomerID SQL will read that to mean the CustomerID column within the Orders table.
The INNER JOIN is looking for an exact match for the records you specify with the ON clause...
So, in this example, SQL will first find all the records in the Orders table which have a CustomerID that matches the CustomerID found in the Customers table.
Then, it will look to the SELECT clause and show you the OrderID (from the Orders table), the CustomerName (from the Customers table) and the OrderDate from the Orders table.
If you're using SQL Server Management Studio (SSMS) or some other similar tool, it should display your results - kindof like a virtual table... but it doesn't actually create a table.
Hope that helps,
Russell
There are INNER AND OUTER JOINS. I think this post sums up those differences well.
What is the difference between "INNER JOIN" and "OUTER JOIN"?
As far as the concept itself you are 'joining' the tables by finding things in common between them based on a shared value in each. If you have ever done a vlookup with Microsoft Excel then you are familiar with this concept. In the query you've posted you are selecting values from both the Orders and Customers table. You can tell which table you are selecting values from by the prefix Orders. or Customers. You can then tell which column by the value following the dot i.e. OrderID, CustomerName, OrderDate.
The way the query knows to pull a conjoined field is if the CustomerID field matches.
So step by step
You are selecting
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
From the table Orders
FROM Orders
You wish to pull corresponding values from Customers. There are numerous join methods and logic that goes with each but you have chosen INNER JOIN explained in the link above.
INNER JOIN Customers
The criteria for joining is CustomerID
ON Orders.CustomerID=Customers.CustomerID;
To use an approximate Excel Vlookup analogy
The Lookup_value is CustomerID
The Table array is ORDERS
The range lookup is the type of join you are performing
SELECT statement returns data, it doesn't create any table not even virtual table whatsoever (if you see any table after executing SELECT statement, thats just a way the returned data presented). Create and modify table done using DDL (Data Definition Language) statements when SELECT is one of DML (Data Manipulation Language) [reference].
Join statement explained very well here, from basic use to more advance with illustration.