add a WHERE clause and group by : Query Select « Query « SQL Server / T-SQL Tutorial






4>
5>
6> CREATE TABLE Employees (
7>      EmployeeID int NOT NULL ,
8>      LastName nvarchar (20) NOT NULL ,
9>      FirstName nvarchar (10) NOT NULL ,
10>     Title nvarchar (30) NULL ,
11>     TitleOfCourtesy nvarchar (25) NULL ,
12>     BirthDate datetime NULL ,
13>     HireDate datetime NULL ,
14>     Address nvarchar (60) NULL ,
15>     City nvarchar (15) NULL ,
16>     Region nvarchar (15) NULL ,
17>     PostalCode nvarchar (10) NULL ,
18>     Country nvarchar (15) NULL ,
19>     HomePhone nvarchar (24) NULL ,
20>     Extension nvarchar (4) NULL ,
21>     Photo image NULL ,
22>     Notes ntext NULL ,
23>     ReportsTo int NULL ,
24>     PhotoPath nvarchar (255) NULL
25>
26> )
27> GO

1>
2>
3>    SELECT ReportsTo AS Manager, COUNT(*) AS Reports
4>    FROM Employees
5>    WHERE EmployeeID != 5
6>    GROUP BY ReportsTo
7>
8> GO
Manager     Reports
----------- -----------

(0 rows affected)
1>
2> drop table Employees;
3> GO
1>








1.1.Query Select
1.1.1.The basic syntax of the SELECT statement
1.1.2.The operators you can use in a search condition
1.1.3.Both uppercase and lowercase statements are acceptable, a query could be written as follows:
1.1.4.Operator Precedence
1.1.5.asterisk (*) specifies all columns of the named tables in the FROM clause
1.1.6.A SELECT statement that retrieves rows with non-zero values
1.1.7.A SELECT statement that retrieves two columns and a calculated value for a specific Billing
1.1.8.retrieve specific columns
1.1.9.A SELECT statement that retrieves three columns from each row, sorted in descending sequence by Billing total
1.1.10.A SELECT statement that returns an empty result set
1.1.11.SELECT 1.0 + 3.0 / 4.0 -- Returns 1.75
1.1.12.SELECT (1.0 + 3.0) / 4.0 -- Returns 1.00
1.1.13.The specification column_name [as] column_heading
1.1.14.Table alias
1.1.15.add a WHERE clause and group by
1.1.16.A SELECT statement that uses the CHAR function to format output
1.1.17.Use local variable in a select statement
1.1.18.Using SELECT to Create a Script