Click here to Skip to main content
14,982,215 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I was wondering if someone could give me an example of a SQL Sub query with a table schema and a reason for doing it.

I saw this at my previous company SELECT ( Select * from ) ...something like that. I have never used sub queries before and I'm wondering what purpose they serve .. I have no idea what the previous programmer who wrote the sub query was trying to do but I know that you can write sub queries. I just have no idea why .

Any example would be appreciated.

If You want a column or a value or a single where condition data then you can use sql sub query instead of writing complex join etc.

Ex 1. Select name,gender,(Select top 1 schoolname from abc where xyzid = 1) from xyz where id=1

Ex 2. Select name,gender from xyz where id=(select max(id) from xyz)

Ex 3. Select abcxyz.Name from (select id,Name from xyz
where id=(select max(id) from xyz) ) abcxyz;

You can learn from [^here]
[no name] 2-Aug-11 10:42am
walterhevedeich 1-Aug-11 19:26pm
Nice link.
[no name] 2-Aug-11 10:42am
thatraja 2-Aug-11 10:44am
You welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900