0% found this document useful (0 votes)
6 views

Lab 1_INTRODUCTION TO SQL.

The document outlines the first laboratory work focused on Microsoft SQL Server and SQL queries, specifically using the SELECT statement with a model database called 'Sample_db'. It includes examples of SQL queries, explanations of aggregate functions, JOIN operations, and various SQL concepts, as well as exercises for practice. Additionally, it provides guidance on using Microsoft SQL Server Management Studio and troubleshooting common query issues.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab 1_INTRODUCTION TO SQL.

The document outlines the first laboratory work focused on Microsoft SQL Server and SQL queries, specifically using the SELECT statement with a model database called 'Sample_db'. It includes examples of SQL queries, explanations of aggregate functions, JOIN operations, and various SQL concepts, as well as exercises for practice. Additionally, it provides guidance on using Microsoft SQL Server Management Studio and troubleshooting common query issues.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LABORATORY WORK 1.

INTRODUCTION TO SQL AND MS SQL SERVER

Formulation of the problem

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.

Topics for study

• Basic concepts of relational and SQL-oriented databases.


• SELECT statement.
• Aggregate and built-in functions of Microsoft SQL Server.
• The structure of the training database "Sample_db".
• Working in Microsoft SQL Server Management Studio

Examples.
Let's consider a simple example: determine the minimum order amount for the product "SB ENERGY BAR-
6PACK"

SELECT MIN(total) AS min_total


FROM item JOIN product ON item.product_id = product.product_id
WHERE description = 'SB ENERGY BAR-6 PACK'

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:

SELECT first_name, last_name, commission/salary AS rate


FROM employee
WHERE commission IS NOT NULL
AND commission/salary =
(SELECT MAX(commission/salary)
FROM employee
WHERE commission IS NOT NULL)
first_name last_name rate
--------------- --------------- ---------------------------------------
KENNETH MARTIN 1.1200000000

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

first_name last_name rate


--------------- --------------- ---------------------------------------
KENNETH MARTIN 1.1200000000
KAREN SHAW 0.9600000000
RAYMOND PORTER 0.7200000000
LIVIA WEST 0.6666666666
PAUL ROSS 0.6153846153
CYNTHIA WARD 0.4000000000
DANIEL PETERS 0.2400000000
GREGORY LANGE 0.2400000000
KEVIN ALLEN 0.1875000000
MARY TURNER 0.0000000000

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:

SELECT TOP 1 first_name, last_name, commission/salary AS rate


FROM employee
WHERE commission IS NOT NULL
ORDER BY commission/salary DESC

first_name last_name rate


--------------- --------------- ---------------------------------------
KENNETH MARTIN 1.1200000000
(1 row(s) affected)

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:

SELECT TOP 1 WITH TIES first_name, last_name, commission/salary as rate


FROM employee
WHERE commission IS NOT NULL
ORDER BY commission/salary DESC

first_name last_name rate


--------------- --------------- ---------------------------------------
KENNETH MARTIN 1.1200000000

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:

SELECT AVG(quantity) AS average_quantity FROM item

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:

SELECT AVG(1.0*quantity) AS average_quantity FROM item

average_quantity
---------------------------------------
81.881918

The same must be remembered when dividing.

Examples of questions on the mandatory part:


• Explain how written queries work.
• Explain about the JOIN operation and its various varieties.
• Explain aggregate functions, GROUP BY and HAVING clauses.
• How to select only the unique values of a column?
• How to sort in ascending / descending order by the value of any column?
• How do aggregate functions behave with respect to undefined values?
• Explain about set-theoretic operations in SQL.
• What is the difference between UNION and UNION ALL?
• What is the difference between COUNT (*) and COUNT (field)?
• How to count the number of unique values in a column?
• How can an undefined value be tested?
• Tell about the predicate LIKE.
• How can you select only a certain number of rows?
• How is a SQL table different from a relationship?

Examples of additional questions:


• Fix incorrectly working request (s).
• Simplify one or more queries.
• Round off the resulting value to 3 decimal places.
• Round off a real number to an integer without zeros after the dot.
• Rewrite the query without using the MAX (MIN) function.
• Change the data output format (for example, date and time format).
• Write or modify a request for a formulated task

You might also like