Presents your SQL SERVER E-NEWSLETTER for May 13, 2003 <-------------------------------------------> DISCOVER OTHER USES FOR THE ASTERISK In most SELECT statements, the asterisk (*) is a method to reference all columns in a given table. Yet the asterisk can also help you get information about a table's structure and can be used as a method of filtering data in a query. When the asterisk is used without a qualifier, the order of the columns in the result set is the same order in which they were specified in the CREATE TABLE, ALTER TABLE, or CREATE VIEW statements. In other words, even if a table has been altered, the asterisk will return the latest columns in the most recent table state. In this example, let's take a small table like Shippers from the Northwind database. Shippers has three rows, each with a ShipperID, CompanyName, and Phone column, in that order. If we use the following query, our result set will return data in the same order. USE NORTHWIND GO SELECT * FROM SHIPPERS GO ShipperID CompanyName Phone ----------- ---------------- ------------ 1 Speedy Express (503) 555-9831 2 United Package (503) 555-3199 3 Federal Shipping (503) 555-9931 (3 row(s) affected) Used with a table or view qualifier, the asterisk will return all the columns in that table or view. This is useful in more complex queries when you need specific conditions to be met for the returned data. For example, using the Northwind database, we want all columns in the Shippers table returned that meet our JOIN requirements. USE Northwind GO SELECT Orders.OrderID, Shippers.* FROM Shippers JOIN Orders ON (Shippers.ShipperID = Orders.ShipVia) ORDER BY Orders.OrderID GO OrderID ShipperID CompanyName Phone ------ --------- ---------------- ------ 10248 3 Federal Shipping (503) 555-9931 10249 1 Speedy Express (503) 555-9831 11076 2 United Package (503) 555-3199 11077 2 United Package (503) 555-3199 (830 row(s) affected) ----------------------------------------