0
CREATE TABLE car (id INT, name1 CHAR(15))
CREATE TABLE sales (sid INT, cid INT, year1 INT)

INSERT INTO car
VALUES (1, 'vento'), (2, 'vento'), (3, 'baleno'), (4, 'swift')
INSERT INTO sales
VALUES (1, 1, 2017), (2, 3, 2018), (3, 3, 2017), (5, 4, 2017)

--verify
--SELECT * FROM car
--SELECT * FROM sales

--1st query
SELECT sid, cid, year1, name1
FROM sales master
INNER JOIN car ON cid = id 

--2nd query
SELECT sid, cid, year1, name1
FROM sales
INNER JOIN car ON cid = id 

What is the difference between the 1st and the 2nd query?

What is the purpose of "master join" and when should we use it?

Brien Foss
  • 3,336
  • 3
  • 21
  • 31
karthik sonu
  • 23
  • 1
  • 4
  • 1
    I think `master` is an alias for table `sales`. For example you can write `select * from sales AS m` or `select * from sales m`. – Sergey Menshov Mar 01 '18 at 04:33
  • @Leran2002 You *think* it is? – philipxy Apr 09 '19 at 05:05
  • @philipxy Yes, the first version of question was "master join in sql". It was edited later. Look at https://stackoverflow.com/posts/49042605/revisions – Sergey Menshov Apr 09 '19 at 06:31
  • @Leran2002 Thanks. But I don't understand the point of your comment or the significance of the first (title) version being what it was. What I was trying to say was, it's clear from the code that master *is* an alias, so the "I think" should be dropped & you shouldn't have any of the doubt that "I think" suggests, although that's not it's literal meaning. But maybe you're just not sure. – philipxy Apr 09 '19 at 06:53
  • @philipxy Ok, I understood you. :) You're right the word `think` was unnecessary there. I'll try not to use such expressions. – Sergey Menshov Apr 09 '19 at 07:35
  • @Leran2002 It's interesting--Sometimes "I think" means "it is my opinion that" & is important to making statements more palatable, just as "it is my opinion that" is, even though, by simply stating something, obviously one thinks it & one is expressing one's opinion. Maybe bald statements are taken with implicit "It seem to me that everybody or anyone would think that it is evident that". But in order to force myself to have justification & clarity I have for some time (longer than my time on SO) just baldly stated what I think. ("I think" often means "I'm not sure".) But it offends people! – philipxy Apr 09 '19 at 09:08
  • @Leran2002 I recently saw an episode of TV show Young Sheldon where the mathematically precocious titular 9-year-old asks his university professor mentor who says his homework answer is flawed to speak to him like a colleague not like a child. The professor pokes the table with his finger & says "This is the stupidest thing I've ever seen!" I was incapacitated with laughter to tears for ten minutes. Like now. That is just how it is in Mathville. (Grandmother: When you speak like that to your colleagues, do they run from the room in tears? Me: Well ... sometimes. Professor: Well ... sometimes.) – philipxy Apr 09 '19 at 09:08
  • @philipxy Thank you for the English lesson. :) It's really interesting and I took a note. I'm glad that my answer could make you laugh. – Sergey Menshov Apr 09 '19 at 09:39
  • @Leran2002 PS This was just posted in a question: ["I think it is TRUE but I cannot justify why."](https://stackoverflow.com/q/55589079/3404097) – philipxy Apr 09 '19 at 14:02

2 Answers2

3

Nothing is different, it is functionally the same. To explain, I've qualified the column names to demonstrate that master is just an alias.

I'd highly recommend qualifying the rest of the query as well, since ...car on cid = id works now, but isn't good form because if ever sales or car tables had the same column name, you'd get an error about ambiguity.

Also, decide if you want to use INNER JOIN, LEFT OUTER JOIN, etc.. [Types of joins] because it's more clear what you desire from a maintenance point of view later on.

SELECT master.sid
    ,master.cid
    ,master.year1
    ,c.name1
FROM sales master
INNER JOIN car c ON master.cid = c.id --(1st)

SELECT s.sid
    ,s.cid
    ,s.year1
    ,c.name1
FROM sales s
INNER JOIN car c ON s.cid = c.id --(2nd)
Brien Foss
  • 3,336
  • 3
  • 21
  • 31
1

There is no concept called Master Join in sql.

SELECT 
sid, cid, year1, name1
FROM sales master
INNER JOIN car 
ON 
cid = id 

The above query takes master as alias name for sales table and does the inner join between sales table and car table you can refer cid using alias (master) as below:

SELECT 
sid, cid, year1, name1
FROM sales master
INNER JOIN car 
ON 
master.cid = id 
Asif Raza
  • 3,435
  • 2
  • 27
  • 43
Akhil Boga
  • 21
  • 1