Department of Information Technology
Department of Information Technology
Department of Information Technology
A SELECT statement in SQL enables you to retrieve existing data from a SQL Server
database.
The full syntax of the SELECT statement is complex, but the main clauses can be
summarized as follows:
■ SELECT select_list
■ [INTO new_table_name]
■ FROM table_list
■ [WHERE search_conditions]
■ [GROUP BY group_by_list]
■ [HAVING search_conditions]
■ [ORDER BY order_list [ASC | DESC] ]
The AS Keyword
You can improve the readability of a SELECT statement by giving a table an alias (also
known as a correlation name or range variable). A table alias can be assigned either with
or without the AS keyword:
■ table_name AS table_alias
1
Department of Information Technology
■ table_name table_alias
The HAVING clause is typically used in conjunction with the GROUP BY clause,
although it can be specified without GROUP BY. The HAVING clause specifies more
filters that are applied after the WHERE clause performs its filtering.
The GROUP BY keywords are followed by a list of columns, known as the grouping
columns. The GROUP BY clause restricts the rows of the result set. There is only one
row for each distinct value in the grouping column or columns. Each result set row
contains summary data related to the specific value of its grouping columns.
Typically, the HAVING clause is used with the GROUP BY clause, although HAVING
can be specified separately.
3
Department of Information Technology
This INSERT statement uses the output of the SELECT subquery to provide the data that
will be inserted into the NewBooks table.
Example:
Add the field Telephone No a number data type field that can hold a number up to 8
Digits in length.
Implementation in SQL:
Referring to the tables developed in Experiment no 2 solve the following queries.
a. Find out the customers who stay in an area ‘SA’, or area ‘BI’ or area ‘CH’.
d. Determine the maximum and minimum movie prices. Rename the title as
MAXIMUM and MINIMUM.
g. Calculate the average price for each type that has average price > 150.
h. Calculate the average price of all movies where type is ‘comedy’ or ‘thriller and
price>=150
i. Print the names of all customers whose customer id is between A01 and A05
j. List the various movie types available from the movie table.
k. Find the movies whose price is greater than 150 and less than or equal to 200.
m. Create a table ‘NewCustomer’.Insert the last names and first names of all the
customers into this table using select subquery.
5
Department of Information Technology
n. Print the information of invoice table in the following format for all records :
The Invoice No. Of Customer Id. {cust_id} is {inv_no} and Movie No.
Is {movie_no}.