As opposed to selecting all records, you may be interested in only one particular column whose fields you want to view. To visually select one record of a table, you must use a Query Editor, not the Query Designer. Click the column header:
To select a column, you can replace the WhatField(s) placeholder of our formula with the name of the desired column. For example, to get a list of last names of students, you would execute the following statement: SELECT LastName FROM Registration.Students;
GO
You can also qualify a column by preceding it with the name of the table followed by the period operator. The above statement is equivalent to: SELECT Students.LastName FROM Registration.Students; When you execute the statement, it would display only the column that contains the last names.
If you specify a column to select from a table, every record would come up. This can cause the same value to repeat over and over. Here is an example: This would produce: CREATE DATABASE VideoCollection1;
GO
CREATE TABLE Videos
(
Title nvarchar(50),
Director nvarchar(50),
Rating nvarchar(10),
YearReleased int
);
GO
INSERT INTO Videos
VALUES(N'Her Alibi', N'Bruce Beresford', N'PG-13', 1998),
(N'Two for the Money', N'D.J. Caruso', N'R', 2008),
(N'Memoirs of a Geisha', N'Rob Marshall', N'PG-13', 2006);
GO
INSERT INTO Videos(Rating, Title, Director)
VALUES(N'R', N'Wall Street', N'Oliver Stone'),
(N'G', N'Annie', N'John Huston'),
(N'PG', N'Incredibles (The)', N'Brad Bird'),
(N'PG-13', N'Sneakers', N'Phil Alden Robinson');
GO
SELECT Videos.Rating FROM Videos;
GO
Notice that PG-13 and R are repeated. Sometimes you want to show each value only once. To visually get such a result, if you are working in the Query Designer, click the check box of the desired column in the Diagram pane or select the field in the Column of the Criteria pane. Then, in the Properties window, set the Distinct Values field to Yes:
In the SQL, to get a list of distinct values, put the DISTINCT keyword between SELECT and the name of the column. Here is an example:
In most cases, you would get a better result if you select only one column. Still, you can use as many columns as you want.
To consider more than one column in a statement, you can list them in the WhatField(s) placeholder of our formula, separating them with a comma except for the last column. The syntax you would use is: SELECT Column1, Column2, Column_n FROM WhatObject; For example, to display a list that includes only the names, gender, email address and home phone of records from a table called Students, you would type: SELECT FirstName, LastName, Gender, EmailAddress, HomePhone FROM Registration.Students; You can qualify each column by preceding it with the name of the table followed by the period operator. The above statement is equivalent to: SELECT Students.FirstName,
Students.LastName,
Students.Gender,
Students.EmailAddress,
Students.HomePhone
FROM Registration.Students;
You don't have to qualify all columns, you can qualify some and not qualify some others. The above statement is equivalent to: SELECT Students.FirstName,
LastName,
Students.Gender,
EmailAddress,
HomePhone
FROM Registration.Students;
When executed in a Query Editor, this expression would produce:
Once again, remember that you can use an alias name for a table by preceding each column with a letter or a word and a period operator, and then entering the name of the table followed by that letter or word. Here is an example: SELECT std.FirstName,
std.LastName,
std.Gender,
std.EmailAddress,
std.HomePhone
FROM Registration.Students std;
Remember that if the alias is in more than one word, you can include it in either double-quotes or between square brackets. Here are examples: SELECT [Little Angels].FirstName, "Little Angels".LastName, [Little Angels].HomePhone, "Little Angels".ParentsNames FROM Registration.Students [Little Angels]; GO
If you have more than one table in your database, you can use a statement that selects any field(s) you want from those tables. Neither the tables nor the columns need to have anything in common. The formula to follow is: SELECT WhatField(s) FROM Table_1, Table_2, Table_n You start with SELECT followed by the list of fields from the tables. If the tables have columns with different names, you can simply list the name of each column. Consider the following tables: CREATE DATABASE Exercise;
GO
USE Exercise;
GO
CREATE TABLE Employees
(
[Empl #] nchar(7),
[First Name] nvarchar(20),
[Last Name] nvarchar(20),
[Hourly Salary] money
);
GO
CREATE TABLE Products
(
Number int,
Name nvarchar(50),
UnitPrice money,
);
GO
INSERT INTO Employees
VALUES(N'207-025', N'Julie', N'Flanell', 36.55),
(N'926-705', N'Paulette', N'Simms', 26.65),
(N'240-002', N'Alexandra', N'Ulm', 12.85),
(N'847-295', N'Ellie', N'Tchenko', 11.95);
GO
INSERT INTO Products
VALUES(217409, N'Short Black Skirt', 55.85),
(284001, N'Pencil Skirt', 49.00);
GO
Here is an example of selecting columns from those tables: SELECT [Empl #], [First Name], [Last Name], Name, UnitPrice FROM Employees, Products; GO When you select fields from different tables, in the result, each of the records of the first table would display, each showing the first record (combination of the selected columns) of the second table. Then each of the records of the first table would show again, followed by the second record (combination of the selected columns) of the second table. This will continue until all records of the second table have displayed. Consequently, the resulting query would contain (Number of Records of First Table) x (Number of Records of Second Table). For example, if the first table contains 4 records and the second table contains 2 records, the statement would produce 4 x 2 = 8 records. Therefore, the above statement would produce:
Imagine your tables have each a column with the same name: DROP TABLE Employees;
GO
DROP TABLE Products;
GO
CREATE TABLE Employees
(
[Empl #] nchar(7),
Name nvarchar(50),
[Hourly Salary] money
);
GO
CREATE TABLE Products
(
Number int,
Name nvarchar(50),
UnitPrice money,
);
GO
INSERT INTO Employees
VALUES(N'207-025', N'Julie Flanell', 36.55),
(N'926-705', N'Paulette Simms', 26.65),
(N'240-002', N'Alexandra Ulm', 12.85),
(N'847-295', N'Ellie Tchenko', 11.95);
GO
INSERT INTO Products
VALUES(217409, N'Short Black Skirt', 55.85),
(284001, N'Pencil Skirt', 49.00);
GO
When selecting the columns, you must qualify at least the column(s) with the same name. Otherwise you would receive an error. Therefore, the above Name columns can be accessed as follows: SELECT [Empl #], empl.Name, prod.Name, UnitPrice FROM Employees empl, Products prod; GO This would produce:
Of course, you can qualify all columns of the tables. Here are examples: DROP TABLE Employees;
GO
DROP TABLE Products;
GO
CREATE TABLE Employees
(
[Empl #] nchar(7),
[First Name] nvarchar(20),
[Last Name] nvarchar(20),
[Hourly Salary] money
);
GO
CREATE TABLE Products
(
Number int,
Name nvarchar(50),
UnitPrice money,
);
GO
INSERT INTO Employees
VALUES(N'207-025', N'Julie', N'Flanell', 36.55),
(N'926-705', N'Paulette', N'Simms', 26.65),
(N'240-002', N'Alexandra', N'Ulm', 12.85),
(N'847-295', N'Ellie', N'Tchenko', 11.95);
GO
INSERT INTO Products
VALUES(217409, N'Short Black Skirt', 55.85),
(284001, N'Pencil Skirt', 49.00);
GO
SELECT empl.[Empl #], empl.[First Name], empl.[Last Name],
prod.Name, prod.UnitPrice
FROM Employees empl, Products prod;
GO
Just as we have used only two tables, you can select records from three or more tables, following the same formula.
In the Query Editor or the Query Designer, you can show the records of as many tables as you want and those tables don't need to have anything in common. They don't even have to belong to the same database. Consider the following database named Cruise equipped with a table named Cabins: CREATE DATABASE Cruise;
GO
USE Cruise;
GO
CREATE TABLE Cabins(
CabinType nvarchar(20) not null,
Deck nchar(20),
Size int,
Rate1Passenger money,
Rate2Passengers money);
GO
INSERT INTO Cabins -- Size in sq/ft
VALUES(N'Inside', N'Riviera', 215, 289.00, 578.00),
(N'Outside', N'Riviera', 185, 319.00, 638.00),
(N'Outside', N'Riviera', 225, 389.00, 778.00),
(N'Suite', N'Verandah', 295, 1009.00, 2018.00),
(N'Inside', N'Upper', 185, 379.00, 758.00),
(N'Inside', N'Main', 215, 359.00, 718.00),
(N'Outside', N'Riviera', 185, 349.00, 698.00),
(N'Suite', N'Main', 300, 885.00, 1680.00);
GO
Here is another database named Video Collection with a table named Videos: CREATE DATABASE VideoCollection
GO
USE VideoCollection
GO
CREATE TABLE Videos
(
Title nvarchar(50),
Director nvarchar(50),
WideScreen bit,
Rating nchar(10),
YearReleased int
)
GO
INSERT INTO Videos
VALUES(N'War of the Roses (The)', N'Dany de Vito', 0, N'R', 2001),
(N'Memoirs of a Geisha', N'Rob Marshall', 1, N'PG-13', 2006),
(N'Last Castle (The)', N'Rod Lurie', 1, N'', 2001),
(N'Sneakers', N'Phil Alden Robinson', 1, N'PG-13', 2003);
GO
To show the records of more than one table, in the Query Editor, write a SELECT statement for each table and execute it. If the tables belong to different databases, make sure you indicate this. Here is an example: USE Cruise;
GO
SELECT CabinType AS [Cabin Type], Deck, Size AS [Size in sq/ft],
Rate1Passenger AS [Rate for 1 Passenger],
Rate2Passengers AS [Rate for 2 Passengers]
FROM Cabins;
GO
USE VideoCollection;
GO
SELECT Title, Director, WideScreen As [Has Wide Screen],
Rating, YearReleased AS [(c) Year]
FROM Videos;
GO
When executed, the lower part of the window displays the records of the tables, each table on its own part:
|
|
|||||||||||||||||||||
|
|