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

SQL ASSIGNMENT

Uploaded by

pohankargaurav06
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

SQL ASSIGNMENT

Uploaded by

pohankargaurav06
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

QUERRY 1 : Create a table named customers with the following columns and

constraints:

customer_id (Primary Key, Auto Increment, Integer)


first_name (Not Null, Varchar(50))
last_name (Not Null, Varchar(50))
email (Unique, Varchar(100))
date_of_birth (Date, Not Null)
phone_number (Varchar(15))

Answer:
CREATE TABLE customer
( customer_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
date_of_birth DATE NOT NULL,
phone_number VARCHAR(15) NOT NULL
);

QUERRY 2 : Create a table named orders with the following columns and
constraints:

order_id (Primary Key, Auto Increment, Integer)


order_date (Date, Not Null)
amount (Decimal(10,2), Not Null)
customer_id (Integer, Foreign Key referencing customers table)

Answer:

CREATE TABLE orders (


order_id INT
AUTO_INCREMENT PRIMARY KEY,
order_date DATE NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
customer_id INT,
CONSTRAINT fk_custid FOREIGN KEY (customer_id)
REFERENCES customer (customer_id)
);

QUERRY 3: Add a new column address (Varchar(255)) to the customers table.

Answer:

ALTER TABLE customer


ADD COLUMN address VARCHAR(255);
QUERRY 4: Modify the phone_number column in the customers table to
increase its length to Varchar(20).

Answer:

ALTER TABLE customer


MODIFY COLUMN phone_number VARCHAR(20);

QUEERY 5:Rename the email column in the customers table to


customer_email.

Answer:

ALTER TABLE customer


CHANGE COLUMN email customer_email VARCHAR(100);

QUEERY 6:Rename the orders table to customer_orders.

Answer:

RENAME TABLE orders TO customer_orders;

QUERRY 7: Add gender in customer table

Answer:

ALTER TABLE customer


ADD COLUMN gender VARCHAR(100);

QUEERY 8:Drop the phone_number column from the customers table.

Answer:

ALTER TABLE customer DROP column phone_number;

QUEERY 9:Add a new unique constraint on the combination of first_name and


last_name in the customers table to ensure no two customers have the same
first and last name combination.

Answer:

ALTER TABLE customer


ADD CONSTRAINT uq_full_name UNIQUE (first_name,last_name);

QUERRY 10: Modify the foreign key constraint on the customer_id column in
the customer_orders table to NO ACTION on delete.

Answer:
ALTER TABLE customer_orders DROP FOREIGN KEY fk_custid;
ALTER TABLE customer_orders
ADD CONSTRAINT fk_custid FOREIGN KEY (customer_id)
REFERENCES customer (customer_id)
ON DELETE NO ACTION;

QUERRY 11: Insert five new rows into the customers table with relevant
details.

INSERT INTO customers (first_name, last_name, email, date_of_birth,


gender)
VALUES
('Emily', 'Clark', '[email protected]', '1991-05-20', 'F'),
('George', 'Anderson', '[email protected]', '1983-11-11', 'M'),
('Hannah', 'Harris', '[email protected]', '1992-03-14', 'F'),
('Ivy', 'Roberts', '[email protected]', '1987-07-05', 'F'),
('Jacob', 'Martinez', '[email protected]', '1980-09-09', 'M');

Answer:

INSERT INTO customer (first_name, last_name, customer_email,


date_of_birth, gender)
VALUES
('emily', 'Clark', '[email protected]', '1991-05-20', 'F'),
('George', 'Anderson', '[email protected]', '983-11-11', 'M'),
('Hannah', 'Harris', '[email protected]', '1992-03-14', 'M'),
('Ivy', 'Roberts', '[email protected]', '1987-07-05', 'F'),
('Jacob', 'Martinez', '[email protected]', '1980-09-09', 'M');

QUEERY 12:

Insert five new rows into the orders table with relevant details,
ensuring each order is associated with a valid customer.
INSERT INTO orders (order_date, amount, customer_id)
VALUES
('2023-06-15', 120.50, 1),
('2023-06-18', 45.00, 2),
('2023-06-20', 89.99, 3),
('2023-06-25', 200.75, 4),
('2023-06-30', 150.00, 5);

Answer:
INSERT INTO customer_orders (order_date, amount, customer_id)
VALUES
('2023-06-15', 120.50, 1),
('2023-06-18', 45.00, 2),
('2023-06-20', 89.99, 3),
('2023-06-25', 200.75, 4),
('2023-06-30', 150.00, 5);
QUEERY 13: Update the amount of all orders in the orders table to be
increased by 10% for customers whose customer_id is less than 5.

Answer:

UPDATE customer_orders
SET amount = amount*1.10
WHERE customer_id < 5;

QUEERY 14:

Change the gender of all customers in the customers table to 'Other'


where the first_name starts with the letter 'I'.

Answer:

UPDATE customer
SET gender = 'Other'
WHERE first_name LIKE 'I%';

QUEERY 15:

Update the email of the customer with first_name 'Hannah' and last_name
'Harris' to '[email protected]'

Answer:

UPDATE customer
SET customer_email = '[email protected]'
WHERE first_name = 'Hannah' And last_name = 'Harris';

QUEERY 16:

Change the order_date to '2023-07-01' for all orders where the amount is
greater than 100.

UPDATE customer_orders
SET order_date = '2023-07-01'
WHERE amount > 100;

QUEERY 17:

Update the last_name to 'Green' for all customers whose first_name is


'Emily'.

Answer:

UPDATE customer
SET last_name = 'Green'
WHERE first_name = 'Emily'
QUERRY 18:

Delete all orders from the orders table where the amount is less than 50.

Answer:

DELETE FROM customer_orders


WHERE amount < 50;

QUERRY 19:

Remove the customer record from the customers table whose email is
'[email protected]'

Answer:

DELETE FROM customer


WHERE customer_email = '[email protected]';

QUERRY 20:

Delete all records from the customers table where the date_of_birth is
before '1985-01-01'

Answer:

DELETE FROM customer


WHERE date_of_birth = '1985-01-01';

OR

Answer:

SELECT * FROM customer


WHERE date_of_birth < '1985-01-01';

You might also like