Lab 1_INTRODUCTION TO SQL.
Lab 1_INTRODUCTION TO SQL.
The first Laboratory work is acquainted with Microsoft SQL Server, Management Studio, and write a simple
SQL-queries using the SELECT statement.
For the model database "Sample_db" 4 requests must be made according to the individual option.
Software Microsoft SQL Server (we recommend using the Express edition) available for free download on the
official website MS SQL Server https://www.microsoft.com/ru-ru/sql-server/sql-server-downloads.
Script for creating and filling a model database and its the description, as well as the content of individual
options are available at file Sample.db
After you have created your queries, you should make sure they are correct using simpler queries.
Examples.
Let's consider a simple example: determine the minimum order amount for the product "SB ENERGY BAR-
6PACK"
min_total
---------------------------------------
2.40
This query uses the MIN aggregate function, the inner conditional join of tables (INNER JOIN or just JOIN),
and the WHERE constraint.
Consider another example: choose the salesperson with the highest commission / salary ratio. Let's first
consider the option with nested subqueries:
You can verify that this query is working correctly using a simpler query:
SELECT first_name, last_name, commission/salary AS rate
FROM employee
WHERE commission IS NOT NULL
ORDER BY commission/salary DESC
The condition (commission IS NOT NULL) is required because the commission column allows undefined
values.
Let us now consider other options for writing this request. For example, you can get rid of subqueries by
applying sorting and limiting the number of results:
The result of executing this query coincides with the result of executing the original variant, but there is a
common mistake here: if there is more than one seller in the database with the maximum commission /
salary ratio, then only one of them will be displayed anyway. This can be easily avoided by using the WITH
TIES clause (see Books Online for more information on the SELECT statement). Correct query option without
using subqueries will look like this:
The following example: select the average annual amount of orders of the buyer "REBOUND SPORTS".
SELECT s/ny AS average
FROM
(SELECT SUM(total) AS s
FROM customer JOIN sales_order
ON customer.customer_id = sales_order.customer_id
WHERE name = 'REBOUND SPORTS') t1,
(SELECT COUNT(DISTINCT YEAR(order_date)) AS ny
FROM customer JOIN sales_order
ON customer.customer_id = sales_order.customer_id
WHERE name = 'REBOUND SPORTS') t2
average
---------------------------------------
1810.866666
The first subquery calculates the total amount of orders for the customer “REBOUND SPORTS”, and the
second one calculates the number of years in which this customer ordered something. Here you should pay
attention to the DISTINCT keyword, which allows you to count the number of non-repeating values.
As a final example, consider a simple query that is often misleading: find the average quantity in the item
table. An obvious solution as follows:
average_quantity
----------------------------------------
81
The problem is that the result of an aggregate function in SQL Server is of the same type as its argument. So,
in this example, you need to convert the values of the integer column quantity:
average_quantity
---------------------------------------
81.881918