Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

Aggregate functions

CREATE TABLE Products
(
    Id INT IDENTITY PRIMARY KEY,
    ProductName NVARCHAR(30) NOT NULL,
    Manufacturer NVARCHAR(20) NOT NULL,
    ProductCount INT DEFAULT 0,
    Price MONEY NOT NULL
);
  
INSERT INTO Products 
VALUES
('iPhone 6', 'Apple', 3, 36000),
('iPhone 6S', 'Apple', 2, 41000),
('iPhone 7', 'Apple', 5, 52000),
('Galaxy S8', 'Samsung', 2, 46000),
('Galaxy S8 Plus', 'Samsung', 1, 56000),
('Mi6', 'Xiaomi', 5, 28000),
('OnePlus 5', 'OnePlus', 6, 38000)

SELECT AVG(Price) AS Average_Price FROM Products
SELECT AVG(Price) FROM Products
WHERE Manufacturer='Apple'
SELECT AVG(Price * ProductCount) FROM Products
SELECT COUNT(Manufacturer) FROM Products
SELECT MIN(Price) FROM Products
SELECT MAX(Price) FROM Products
SELECT SUM(ProductCount) FROM Products
SELECT AVG(DISTINCT ProductCount) AS Average_Price FROM Products
SELECT AVG(ALL ProductCount) AS Average_Price FROM Products
 
PREVIOUS NEXT
Tagged: #Aggregate #functions
ADD COMMENT
Topic
Name
2+1 =