English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can some1 tell me how to use multiple sub-select statements in SQL with an example.

2006-11-29 16:56:17 · 3 answers · asked by Yagneswar (Vamsi) 1 in Computers & Internet Software

3 answers

A simple SELECT statement is the most basic way to query multiple tables. You can call more than one table in the FROM clause to combine results from multiple tables. Here’s an example of how this works:
SELECT table1.column1, table2.column2 FROM table1, table2 WHERE table1.column1 = table2.column1;

In this example, I used dot notation (table1.column1) to specify which table the column came from. If the column in question only appears in one of the referenced tables, you don’t need to include the fully qualified name, but it may be useful to do so for readability.

Tables are separated in the FROM clause by commas. You can include as many tables as needed, although some databases have a limit to what they can efficiently handle before introducing a formal JOIN statement, which is described below.

This syntax is, in effect, a simple INNER JOIN. Some databases treat it exactly the same as an explicit JOIN. The WHERE clause tells the database which fields to correlate, and it returns results as if the tables listed were combined into a single table based on the provided conditions. It’s worth noting that your conditions for comparison don’t have to be the same columns you return as your result set. In the example above, table1.column1 and table2.column1 are used to combine the tables, but table2.column2 is returned.

You can extend this functionality to more than two tables using AND keywords in the WHERE clause. You can also use such a combination of tables to restrict your results without actually returning columns from every table. In the example below, table3 is matched up with table1, but I haven’t returned anything from table3 for display. I’ve merely checked to make sure the relevant column from table1 exists in table3. Note that table3 needs to be referenced in the FROM clause for this example.
SELECT table1.column1, table2.column2 FROM table1, table2, table3 WHERE table1.column1 = table2.column1 AND table1.column1 = table3.column1;

Be warned, however, that this method of querying multiple tables is effectively an implied JOIN. Your database may handle things differently, depending on the optimisation engine it uses. Also, neglecting to define the nature of the correlation with a WHERE clause can give you undesirable results, such as returning the rogue field in a column associated with every possible result from the rest of the query, as in a CROSS JOIN.
If you’re comfortable with how your database handles this type of statement, and you’re combining two or just a few tables, a simple SELECT statement will meet your needs.

2006-11-29 17:06:41 · answer #1 · answered by cedric_316 2 · 0 0

Ok let suppose a table

AutoID UserName Email
1 luckyumer luckyumer@yahoo.com
2 waqaxi waqaxi@yahoo.com
...
...
...
100 luckyarslan luckyarslan@yahoo.com

[IN SQL SERVER]

as you see there are hundred records and I wish to select record 40 to 50 (10 rows) then I would write

SELECT TOP 10 * FROM (SELECT TOP 50 * FROM TableName ORDER BY AutoID ASC) AS AA ORDER BY AutoID ORDER BY AutoID DESC

2006-11-29 17:00:49 · answer #2 · answered by luckyumer 2 · 0 0

I want to give you the answer but this really is too easy. I am sure someone else will give you the answer anyway but in the mean time here is a little tutorial for you to read. You may even beat someone to the answer. Have fun.

2016-03-29 16:46:22 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers