Does Join Order matter performance - sql

create table Employee(
id int identity(1,1) primary key,
Username varchar(50),
FirstName varchar(50),
LastName varchar(50),
DepartID int
)
create table Departments(
id int identity(1,1) primary key,
DepartmentName varchar(50)
)
Both Join result set is same we get Employee matching and non matching.
Select * from Employee E
Left Join
Departments D
ON D.ID= E.DepartID
Select * from Departments D
Right Join Employee E
ON E.DepartID =D.ID

Both of your query are indeed completely interchangeable. And should be no difference in performance nor result.
Most people only use LEFT JOIN since it seems more intuitive, and it's universal syntax - I don't think all RDBMS support RIGHT JOIN. Not all RDBMSs support SQL. For example, SQLite 3 doesn't implement RIGHT and FULL OUTER JOIN.

Related

how Inner join work on two foreign key from single table

I am working on Bus route management system , I made two table first one is Cities and second one is route have following queries
CREATE TABLE Cities
(
ID NUMBER GENERATED ALWAYS AS IDENTITY(START with 1 INCREMENT by 1) PRIMARY KEY,
Name Varchar(30) not null,
)
CREATE TABLE route
(
ID NUMBER GENERATED ALWAYS AS IDENTITY(START with 1 INCREMENT by 1) PRIMARY KEY,
Name Varchar(30) not null,
from NUMBER not null,
to NUMBER NOT NULL,
CONSTRAINT FROM_id_FK FOREIGN KEY(from) REFERENCES Cities(ID),
CONSTRAINT TO_id_FK FOREIGN KEY(to) REFERENCES Cities(ID),
)
i am joining the table through inner join
select CITIES.Name
from CITIES
inner join ROUTES on CITIES.ID=ROUTES.ID
but it show single column as
Name
-----------
but i want result as
from | to
------------------------
what is possible way to do this using inner join
I suspect you need something like the following:
select r.Name, cs.Name SourceCity, cd.Name DestinationCity
from routes r
join cities cs on cs.id = r.from
join cities cd on cd.id = r.to
Hope is working for you
select CITIES.Name,ROUTES.from,ROUTES.to
from CITIES inner join ROUTES on CITIES.ID=ROUTES.ID

How do I prevent duplicate table names when using cross join?

I have the following tables in a SQL database:
t_customers
ID_operator int primary key auto_incr
firstname varchar(30)
lastname varchar(30)
email varchar(100)
t_operator
ID_operator int primary key auto_incr
firstname varchar(30)
lastname varchar(30)
course varchar(10)
I have another table, which represents an order, in which I join both fields when querying another field:
SELECT
*, // table t_orders
t_customers.firstname,
t_customers.lastname,
t_operator.firstname AS operator_firstname,
t_operator.lastname AS operator_lastname
FROM
t_orders
CROSS JOIN
t_customers, t_operator
WHERE
id_orders IS 1;
I have tried to alleviate this by using the AS keyword and the new fields do get added, but I still have 2 fields named "firstname" and "lastname" in my query, preventing me from working with it correctly. Is there any solution besides renaming the tables?
Are you trying to do something like this?
SELECT o.*, // table t_orders
c.firstname, c.lastname,
op.firstname AS operator_firstname,
op.lastname AS operator_lastname
FROM t_orders o JOIN
t_customers c
ON o.id_customer = c.id_customer JOIN
t_operator op
ON c.id_operator = c.id_operator
WHERE id_orders = 1;

Trying to do a three table join displaying all columns

I am trying to do a three table join to display all columns. Only two of the tables have a key in common. Below is the code for the tables I already have:
CREATE TABLE Faculty (
FacultyID int,
FirstName varchar(30),
LastName varchar(30),
EMail varchar(60),
BirthDate DATE,
Numbr_Courses int,
PRIMARY KEY (FacultyID)
);
CREATE TABLE Courses(
CourseID int,
CourseDisc varchar(4),
CourseNum varchar(4),
NumbrCred varchar(1),
FirstYrOffered int,
CourseTitle varchar(75),
PRIMARY KEY (CourseID)
);
CREATE TABLE Faculty_Courses(
InstanceID int,
FacultyID int,
CourseDisc varchar(4),
CourseNum varchar(4),
CourseTitle varchar(75),
PRIMARY KEY (InstanceID),
FOREIGN KEY (FacultyID) REFERENCES Faculty(FacultyID)
);
The two tables that have the same key is Faculty and Faculty_Course. I have attempted one peice of code that only gave me back an error. I guess I am having real trouble understanding how to do proper code for joining tables. My attempted code is below:
SELECT Faculty.*, Faculty_Courses.*
FROM Faculty INNER JOIN Courses
ON Faculty.FacultyID=Faculty_Courses.FacultyID
This gave me back the following error:
ERROR 1066 (42000): Not unique table/alias: 'Faculty'
Any help will be appreciated.
I would change your Faculty_Courses table:
CREATE TABLE Faculty_Courses(
InstanceID int,
FacultyID int,
CourseID int,
PRIMARY KEY (InstanceID),
FOREIGN KEY (FacultyID) REFERENCES Faculty(FacultyID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
And then join all three tables together:
SELECT F.*, FC.*, C.*
FROM Faculty F
INNER JOIN Faculty_Courses FC ON F.FacultyID = FC.FacultyID
INNER JOIN Courses C ON C.CourseID = FC.CourseID
You probably only really want some of the columns from F (Faculty) and C (Courses) and can mostly ignore the FC columns as they are just used for mapping between F and C
I'm surprised your factory_courses table doesn't have a reference to courses.
Either way, with your current query, you are selecting from faculty and courses but aliasing faculty_courses -- you cannot do that. This is what your current query should look like:
select *
from faculty f
join faculty_courses fc on f.facultyid = fc.facultyid
To join the 3rd table, if you had a courseid in your faculty_courses table, perhaps something like this:
select *
from faculty f
join faculty_courses fc on f.facultyid = fc.facultyid
join courses c on fc.courseid = c.courseid
A Visual Explanation of SQL Joins
You're doing a join on two tables without actually linking two columns from those tables; joining Faculty and Courses using columns from Faculty and Faculty_Courses.
Since no column exists with unique values linking Courses with any other table, you cannot perform a join with Courses.
So you should join Faculty and Faculty_courses:
SELECT Faculty.*, Faculty_Courses.*
FROM Faculty INNER JOIN Faculty_Courses
ON Faculty.FacultyID=Faculty_Courses.FacultyID

SELECT value from a second level foreign key table

Greetings fellow Earthlings,
I have a problem. Let me start by laying out my table structure:
CREATE TABLE Person
(
id varchar(50) NOT NULL PRIMARY KEY,
name varchar(50) NOT NULL,
adress varchar(50) NOT NULL references Adress(id)
)
CREATE TABLE Adress
(
id varchar(50) NOT NULL PRIMARY KEY,
addressName varchar(50),
city varchar(50),
aState varchar(50),
linkToCountry varchar(50) references Country(id)
)
CREATE TABLE Country
(
id varchar(50) NOT NULL PRIMARY KEY,
countryName varchar(50)
)
What I want to achieve is: select a person 'name' along with their 'addressName' and the 'countryName' they're from.
I know that this is a joining related issue but I can't seem to figure this one out.
So any help from people who are well versed on SQL?
Would appreciate it very very much any one has links to advance sql joining so I can familiarize myself with it.
You can get the result using simple join as below. This will retrun the person name with address name, and country name. However it returns only those person names which has an address record in the address table and country record in the country table. If you want to retrieve all the persons irrespective of whether address/country exists or not, you need to use left join.
SELECT Person.Name, Address.addressname,Country.countryName
FROM Person
JOIN Address on Person.address = Address.Id
JOIN Country ON Address.linkToCountry = Country.id
Try this:
SELECT p.name, a.addressName, c.countryName
FROM Person p
INNER JOIN Adress a ON p.adress = a.id
LEFT OUTER JOIN Country c ON a.linkToCountry = c.id

Conditional JOIN between tables?

I have two table and I should write a select query which join this two table but I do not know what is conditional join between this two tables?
Can some body say what is?
TABLE ParameterRegistration
(
RegistrationTime DATETIME,
PatiNo VARCHAR(12),
Source VARCHAR(64),
Code VARCHAR(64),
NameOfCodingSystem VARCHAR(64) NULL,
Name VARCHAR(64),
ValueType CHAR(2) NULL,
NumericValue INT NULL,
StringValue VARCHAR(64) NULL,
TextValue TEXT NULL,
Unit VARCHAR(64) NULL,
UnitCode VARCHAR(64) NULL,
UnitCodingSystem VARCHAR(64) NULL,
Remark VARCHAR(255) NULL,
CreateDate DATETIME,
CreateUserId T_USER_ID
)
and
TABLE External
(
ModDate DATETIME,
ModUserId VARCHAR(12),
UbMem VARCHAR(64),
Code VARCHAR(64),
Name VARCHAR(64),
Service VARCHAR(64),
NameOfCodingSystem VARCHAR(64) NULL,
)
You can only properly join those two tables if they have a relation of some kind.
Check THIS ARTICLE for some info.
Assuming that in your case, the tables are related with the columns Code, NameOfCodingSystem and Name you can make a join like this:
select p.*, e.* from ParameterRegistration p
inner join External e on p.Code = e.Code and
p.NameOfCodingSystem = e.NameOfCodingSystem and
p.Name = e.Name
When joining tables you have to join on fields that are related. Since you did not provide a lot of information it looks like you have 3 fields that possibly could be joined on.
NameOfCodingSystem VARCHAR(64)
Name VARCHAR(64)
Code VARCHAR(64)
So you could technically write your query one of these ways:
SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.NameOfCodingSystem = E.NameOfCodingSystem
OR
SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.Name = E.Name
OR
SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.Code = E.Code
OR you can join on all of the fields at the same time
SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.NameOfCodingSystem = E.NameOfCodingSystem AND
P.Name = E.Name AND
P.Code = E.Code
My suggestion would be to study up on JOINs. Here are some resources but there are plenty on the internet:
A Visual Explanation of Joins
SQL Joins
Joins