0

I'm new to SQL programming and having a problem with a query that doesn't return the values but only an empty query with status and city but no values.

S has the following attributes: sNo, sName, status, city SP has the following attributes:sNo, pNo, qty,

Can anyone explain to me why the values don't come up?

SELECT S.status, S.city
FROM S, SP
WHERE S.sNo='Paris' AND SP.sNo=S.sNo;
3
  • 1
    Are you comparing sNo with city? Commented Apr 13, 2019 at 7:07
  • Obviously without seeing your data it's impossible to say for certain but I'd guess that your sNo column maybe contains numbers and not city names. Also you should learn how to use proper INNER JOIN syntax instead of the old fashioned practice of separating the table names with commas Commented Apr 13, 2019 at 7:12
  • On a side note: Don't use comma-separated joins. If you are being taught to do so by a book, tutorial or class, then quit it. Use proper ANSI joins instead. But why are you joining SP at all? You are neither using it in the select clause nor in the where clause. If you want to ensure that an SP record exists for an S row, then use IN or EXISTS instead of a join. Commented Apr 13, 2019 at 12:17

2 Answers 2

3

First, if you are learning SQL, you should be learning SQL correctly. Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.

So, your query should look like this:

SELECT S.status, S.city
FROM S JOIN
     SP
     ON SP.sNo = S.sNo
WHERE S.sNo = 'Paris' ;

Why does this return no results? Here are some reasons:

  • No rows in S and SP share the same city.
  • S.sNo never has the value 'Paris'.
  • One or the other table has no rows (this is a subset of the first reason, but worth highlighting).

In this case, the most likely reason is the sNo never has the value 'Paris'. You have not provided any data, so I can only speculate that you intend:

SELECT S.status, S.city
FROM S JOIN
     SP
     ON SP.sNo = S.sNo
WHERE S.city = 'Paris' ;

But that would make sense.

One thing is suspicious. I would expect a key column called sNo to be numeric. In most databases, a comparison to a string (such as 'Paris') would return a type conversion error. MySQL does implicit conversion. So, if sNo is a number, then the comparison is interpreted as sNo = 0 -- and that could actually return rows if the condition were true.

Sign up to request clarification or add additional context in comments.

2 Comments

Very good feedback, Gordon - thanks a lot! Regarding the use of the comma in the FROM-clause, I have been taught to do it through a course book from Elmasri & Navathe but has seen tutorials where a join is used instead - that confused me. But you are sure right that sNo is numeric, I really must have been tired.
@Triber . . . There are many books (including my own) that would teach you proper SQL syntax.
2

in where clause s.city ='paris'

query

  SELECT S.status, S.city
    FROM S, SP
    WHERE S.city ='Paris' AND SP.sNo=S.sNo;

1 Comment

Well that is obvious now that you write it :D I need to pay more attention to the attributes! Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.