Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

đề pesu23

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

-- Q1:

CREATE TABLE Customer (

CustomerID INT PRIMARY KEY,

LastName VARCHAR(50),

FirstName VARCHAR(50),

Phone VARCHAR(50),

Address VARCHAR(100),

City VARCHAR(50),

Country VARCHAR(50)

CREATE TABLE [Order] (

OrderID INT PRIMARY KEY,

OrderDate DATE,

RequiredDate DATE,

ShippedDate DATE,

Status VARCHAR(50),

Comment VARCHAR(255),

CustomerID INT FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)

CREATE TABLE Product (

ProductCode VARCHAR(50) PRIMARY KEY,

Name VARCHAR(100),

Scale VARCHAR(50),

Vendor VARCHAR(100),

Description TEXT,

BuyPrice DECIMAL(10, 2),

Inventory INT

)
CREATE TABLE OrderDetail (

OrderID INT ,

ProductCode VARCHAR(50),

Qty INT,

Price DECIMAL(10, 2),

PRIMARY KEY (OrderID, ProductCode),

FOREIGN KEY (OrderID) REFERENCES [Order](OrderID),

FOREIGN KEY (ProductCode) REFERENCES Product(ProductCode)

);

--Q2

ALTER TABLE OrderDetail

ADD CONSTRAINT CHK_Qty_GreaterThanZero CHECK (Qty > 0);

-- Q3

--3.1

SELECT CustomerID, Firstname, Lastname, Phone, Address

FROM Customer

WHERE City = 'Da Nang'

--3.2

SELECT OrderID, Qty AS Quantity, Price, Qty * Price AS Total

FROM OrderDetail;

-- 3.3

CREATE VIEW BestSellingItemsInJune AS

SELECT ProductCode, SUM(Quantity) AS TotalQuantity

FROM OrderDetail

WHERE MONTH(OrderDate) = 6

GROUP BY ProductCode
HAVING SUM(Quantity) >= ALL (

SELECT SUM(Quantity)

FROM OrderDetail

WHERE MONTH(OrderDate) = 6

GROUP BY ProductCode

OR (

SELECT COUNT(DISTINCT ProductCode)

FROM OrderDetail

WHERE MONTH(OrderDate) = 6

GROUP BY ProductCode

) = 1;

-- 3.4

SELECT Order1.OrderID, Order1.CustomerID, Order1.RequiredDate, Order1.ShippedDate,


SUM(OrderDetail.Qty * OrderDetail.Price) AS amount

FROM Order1

JOIN OrderDetail ON Order1.OrderID = OrderDetail.OrderID

GROUP BY Order1.OrderID, Order1.CustomerID, Order1.RequiredDate, Order1.ShippedDate

HAVING amount > 500;

Nam

Nguyễn Đức Hoàng Nam

ERD-> moo hinh quan he

SQL: insert, select, delete

function procedure, trigger, view

general scripts

Write an SQL query to create table Account Types with the following properties:

Id - unique number for every type. (Auto incremented, primary key)


Name - the name of the account type, no longer than 50 Unicode characters

(Cannot be null)

CREATE TABLE AccountTypes (

Id INT PRIMARY KEY AUTO_INCREMENT,

Name VARCHAR(50) NOT NULL

);

C.

Write an SQL query to create table Accounts with the following properties:

Id - unique number for every user. (Auto incremented, primary key)

AccountTypeld - references the AccountTypes table (foreign key)

Balance - decimal data type with up to 15 digits including 2 after the decimal point

and a default value of 0 (Not null)

Clientd - references the Clients table (foreign key)

CREATE TABLE Accounts (

Id INT PRIMARY KEY AUTO_INCREMENT,

AccountTypeId INT,

Balance DECIMAL(15, 2) NOT NULL DEFAULT 0,

ClientId INT,

FOREIGN KEY (AccountTypeId) REFERENCES AccountTypes(Id),

FOREIGN KEY (ClientId) REFERENCES Clients(Id)

);

write an sql query to find the first name and the last name of each Client

SELECT FirstName, LastName

FROM Clients;
e. Create a table - Logs (Logld, Accountld, OldBalance, NewBalance). Add a trigger to the

Accounts table that inserts a new record into the Logs table every time the balance on an

account changes.

CREATE TABLE Logs (

LogId INT PRIMARY KEY AUTO_INCREMENT,

AccountId INT,

OldBalance DECIMAL(15, 2),

NewBalance DECIMAL(15, 2),

ChangeDateTime DATETIME DEFAULT CURRENT_TIMESTAMP

);

DELIMITER //

CREATE TRIGGER account_balance_trigger

AFTER UPDATE ON Accounts

FOR EACH ROW

BEGIN

IF NEW.Balance <> OLD.Balance THEN

INSERT INTO Logs (AccountId, OldBalance, NewBalance)

VALUES (OLD.Id, OLD.Balance, NEW.Balance);

END IF;

END //

DELIMITER ;

Write an SQL query to find first and last names of all employees whose last name

"n" or "t".
SELECT FirstName, LastName

FROM Employees

WHERE LastName LIKE '%n%' OR LastName LIKE '%t%';

To find the average salary for each department, you can use the following SQL query:

SELECT DepartmentID, AVG(Salary) AS AverageSalary

FROM Employees

GROUP BY DepartmentID;

Find all the employees whose salary is equal to the maximum salary of their department.

The result should contain 4 columns: EmployeelD, FirstName, LastName, DepartmentID.

Sort by DepartmentID ascending.

SELECT e.EmployeeID, e.FirstName, e.LastName, e.DepartmentID

FROM Employees e

INNER JOIN (

SELECT DepartmentID, MAX(Salary) AS MaxSalary

FROM Employees

GROUP BY DepartmentID

) m ON e.DepartmentID = m.DepartmentID AND e.Salary = m.MaxSalary

ORDER BY e.DepartmentID ASC;

write an SQL query to find first and last name of 10 best paid employees, ordered descending by their
salary

SELECT first_name, last_name

FROM employees
ORDER BY salary DESC

LIMIT 10;

create function UF_FullEmailAddress(

@fName nvarchar(50), @lName nvarchar(50))

returns nvarchar(50)

as

begin

declare @domain nvarchar(50) = N'@softuni.vn'

set @domain = @fName+N'.'

You might also like