264 Part I EXAM PREPARATION As you can

264 Part I EXAM PREPARATION As you can see, there s a table here aliased to P that is actually a SELECT statement, and it s the same SELECT statement that was used earlier. So you can do some interesting things here with copy-andpaste: you can take a query and write another query around it. The problem is, as you can see from the example, the resulting query can be very difficult to format or read. Things that are difficult to format and read also tend to be difficult to optimize, modify, and debug. That s one example of using a query inside another query. Here s another. The IN Operator The IN clause can be used in comparisons inside nearly every SQL statement as an operator. The IN operator takes two arguments a value and a set and checks to see whether the value is part of the set. For example: SELECT * FROM Person WHERE PersonID IN (1, 3, 5) That s a good way to use the IN operator. It can also be used with select queries that return one and only one column, like this: SELECT * FROM Person WHERE PersonID IN (Select PersonID .from PersonAddress) In this case, the SQL statement returns every person who has an address, but it always returns each person only one time. If you run the same query with a join, you get back the same list, assuming each person has only one address. If some of the records in Person have more than one address, you end up with duplicates in the resultset. SELECT Person.* from Person INNER JOIN PersonAddress on PersonAddress.PersonID = .Person.PersonID This returns the same list, but SQL Server executes this differently. SQL Server is very efficient at processing joins, but it s not as efficient at processing IN clauses, so use IN clauses sparingly. Sometimes you need a bit more flexibility in your queries to decide what data to include, based on the data in other fields.

For high quality jboss hosting services please check jboss web hosting website.

Comments are closed.