## Exercise #16 |
||||||||||||||||||

Get pairs of PC models with identical speeds and the same RAM capacity. Each resulting pair should be displayed only once, i.e. (i, j) but not (j, i).Result set: model with the bigger number, model with the smaller number, speed, and RAM. Here is a solution that occurs quite often: Solution 1.12.1
I really have no idea why some users output just the maximum and minimum model for each matching pair of speed and ram values. Maybe the correct result returned by such a query for the main database is misleading. In this task, all models have to be ordered, not just the maximum and the minimum one. Extreme values are mentioned just for the sake of unambiguity, to make it clear a pair of models has to be displayed once, e. g.
Say, if three models 1122, 1121, and 1135 have identical characteristics, the result should look as follows:
The solution presented below is almost correct, albeit somewhat cumbersome. Solution 1.12.2
Here, the subquery S gets the unique pairs of characteristics (speed, memory) coinciding for two computers (SUM (speed)/speed = 2) - the sum of identical values divided by this value yields the number of PCs. However, one could just as well use the following HAVING clause in this case:
The subquery is joined to the PC table PC using this pair of characteristics twice. Thereby, the second join is performed just to order the models (L.model <P.model). The mistake of this solution is, there may be more than two PCs with identical characteristics. In this case, the query under consideration won’t output any of such models. In spite of the fact this solution can be corrected easily, it’s preferable to rewrite it in a less redundant form. One more mistake typical for this exercise is caused by the possible presence of identical computer models in the PC table. Thus, when displaying pairs of PCs you need to remove duplicates. |

aggregate functions
Airport
ALL
AND
AS keyword
ASCII
AVG
Battles
Bezhaev
Bismarck
C.J.Date
calculated columns
Cartesian product
CASE
cast
CHAR
CHARINDEX
Chebykin
check constraint
classes
COALESCE
common table expressions
comparison predicates
Computer firm
CONSTRAINT
CONVERT
correlated subqueries
COUNT
CROSS APPLY
CTE
data type conversion
data types
database schema
DATEADD
DATEDIFF
DATENAME
DATEPART
DATETIME
date_time functions
DDL
DEFAULT
DEFAULT VALUES
DELETE
DISTINCT
DML
duplicates
edge
equi-join
EXCEPT
exercise (-2)
More tags

exercise 19
exercise 23
exercise 32
exercise 37
exercise 39
exercise 46
exercise 54
exercise 55
exercise 56
exercise 57
exercise 7
exercise 70
exercise 8
exercises
EXISTS
FLOAT
FOREIGN KEY
FROM
FULL JOIN
GROUP BY
grouping
Guadalcanal
HAVING
head ships
IDENTITY
IN
income
INFORMATION_SCHEMA
inner join
INSERT
INTERSECT
IS NOT NULL
IS NULL
ISNULL
join operations
laptop
launched year
LEFT
LEFT OUTER JOIN
LEN
LIKE
LTRIM
MAX
MIN
mistakes
money
MySQL
NATURAL JOIN
node
NOT
NOT IN
NULL
NULLIF
number sequences
number-sequence generation
numbering
ON DELETE CASCADE
OR
Oracle
ORDER BY
outcome
Outcomes
outer joins
OVER
paging
Painting
PARTITION BY
Pass_in_trip
PATINDEX
PC
PIVOT
PostgreSQL
predicates
primary key
printer
Product
Ranking functions
recursive CTE
renaming columns
REPLACE
RIGHT
RIGHT JOIN
ROUND
rounding
ROW_NUMBER
ships
sorting
SQL Server
SQL Server 2012
SQL-92
sql-ex.ru
string functions
subquery
SUBSTRING
SUM
tables join
tips and solutions
Torus
Transact-SQL
Trip
TRUNCATE TABLE
type conversion
UNION
UNION ALL
UNKNOWN
UNPIVOT
UPDATE
varchar
WHERE
window functions
WITH
XML
XPath
XQuery

The book was updated

*month ago*