0% found this document useful (0 votes)
0 views3 pages

Mysql 1

The document outlines SQL commands for creating and managing databases for a college, an employee information system, and a bank. It includes the creation of tables, insertion of records, and various queries to retrieve and analyze data. Additionally, it demonstrates the use of aggregate functions and grouping in SQL queries.

Uploaded by

ashutoshdav99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Mysql 1

The document outlines SQL commands for creating and managing databases for a college, an employee information system, and a bank. It includes the creation of tables, insertion of records, and various queries to retrieve and analyze data. Additionally, it demonstrates the use of aggregate functions and grouping in SQL queries.

Uploaded by

ashutoshdav99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

-- Create College Database

CREATE DATABASE IF NOT EXISTS college;


USE college;

-- Create Student Table


CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT NOT NULL
);

-- Insert Students
INSERT INTO student VALUES (1, "Ashutosh", 19);
INSERT INTO student VALUES (2, "Simran", 20);
INSERT INTO student VALUES (3, "Bhanu", 19);
INSERT INTO student VALUES (4, "Shubham", 21);
INSERT INTO student VALUES (5, "Aditya", 20);

-- View Students
SELECT * FROM student;
SHOW DATABASES;
SHOW TABLES;

-- Create xyz_pvt_ltd Database


CREATE DATABASE IF NOT EXISTS xyz_pvt_ltd;
USE xyz_pvt_ltd;

-- Create Employee Info Table


CREATE TABLE employee_info (
id INT DEFAULT 3,
Name VARCHAR(70) NOT NULL,
Salary FLOAT UNSIGNED UNIQUE,
PRIMARY KEY (id, Name),
CONSTRAINT chk_id_name CHECK (id < 7 OR Name = "Akash") -- Renamed constraint to
avoid generic name
);

-- Insert Employees
INSERT INTO employee_info(id, Salary, Name) VALUES (1, 8989.9, "Himanshu");
INSERT INTO employee_info(id, Salary, Name) VALUES (2, 89890.0, "Ujwal");
INSERT INTO employee_info(id, Salary, Name) VALUES (3, 45000.0, "Ram");
INSERT INTO employee_info(id, Salary, Name) VALUES (4, 900000.0, "Vishnu");
INSERT INTO employee_info(id, Salary, Name) VALUES (5, 999.6, "Mayank");
INSERT INTO employee_info(Salary, Name) VALUES (90089.9, "Avneet");
INSERT INTO employee_info(id, Name, Salary) VALUES (7, "Akram", 75608.9);

-- View Employee Info


SELECT * FROM employee_info;
SHOW TABLES;

-- Back to College Database


USE college;

-- Create Passed Student Table


CREATE TABLE passed_student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT UNSIGNED NOT NULL,
marks FLOAT,
grade VARCHAR(1),
city VARCHAR(40),
CHECK (marks >= 35) -- MySQL parses but may not enforce unless engine supports it
);

-- Insert Passed Students


INSERT INTO passed_student(id, name, marks, grade, age, city) VALUES (1, "Ashutosh", 79,
'B', 19, "Delhi");
INSERT INTO passed_student(id, name, marks, grade, age, city) VALUES (2, "Simran", 91,
'O', 20, "Delhi");
INSERT INTO passed_student(id, name, marks, grade, age, city) VALUES (3, "Bhanu", 98,
'O', 19, "Pune");
INSERT INTO passed_student(id, name, marks, grade, age, city) VALUES (4, "Shubham",
82.5, 'A', 21, "Patna");
INSERT INTO passed_student(id, name, marks, grade, age, city) VALUES (5, "Aditya", 89.5,
'A', 20, "Patna");
INSERT INTO passed_student(id, name, marks, grade, age, city) VALUES (6, "Akhil", 99.5,
'O', 20, "Madras");
INSERT INTO passed_student(id, name, marks, grade, age, city) VALUES (7, "Chirag", 69.5,
'C', 18, "Manali");
INSERT INTO passed_student(id, name, marks, grade, age, city) VALUES (8, "Arvind", 27.5,
'F', 18, "Karnal");

-- Queries
SELECT * FROM passed_student;
SELECT name, marks FROM passed_student; -- Fixed column list (was missing comma)
SELECT DISTINCT grade FROM passed_student;
SELECT * FROM passed_student
WHERE (id <= 3 AND grade IN ('A', 'C')) OR (marks > 90 AND city = "Delhi"); -- Replaced
invalid OR expression with IN()
SELECT * FROM passed_student
WHERE (city = "Delhi" AND marks >= 90) OR (id <= 5 AND grade = 'B');
SELECT * FROM passed_student WHERE marks BETWEEN 75 AND 95;
SELECT * FROM passed_student WHERE city IN ("Delhi", "Manali");
SELECT * FROM passed_student WHERE city NOT IN ("Delhi", "Manali");
SELECT * FROM passed_student WHERE marks > 75 LIMIT 3;
SELECT * FROM passed_student ORDER BY city ASC;

-- Aggregate Functions
SELECT MAX(marks) FROM passed_student;
SELECT MIN(marks) FROM passed_student;
SELECT SUM(marks) FROM passed_student;
SELECT AVG(marks) FROM passed_student;
SELECT COUNT(marks) FROM passed_student;

-- Group By
SELECT city FROM passed_student GROUP BY city;
SELECT city, COUNT(name) FROM passed_student GROUP BY city;
SELECT city, AVG(marks) FROM passed_student GROUP BY city ORDER BY AVG(marks) DESC;
SELECT city, grade, AVG(marks) FROM passed_student GROUP BY city, grade ORDER BY
AVG(marks);

-- Create Bank Database


CREATE DATABASE IF NOT EXISTS Bank;
USE Bank;

-- Create Payment Table


CREATE TABLE payment (
customer_id INT UNSIGNED PRIMARY KEY,
customer VARCHAR(100),
mode VARCHAR(50),
city VARCHAR(40)
);

-- Insert Payments
INSERT INTO payment(customer_id, customer, mode, city) VALUES (101, "Olivia Barrett",
"NetBanking", "Portland");
INSERT INTO payment(customer_id, customer, mode, city) VALUES (102, "Ethen Sinclair",
"Credit Card", "Miami");
INSERT INTO payment(customer_id, customer, mode, city) VALUES (103, "Maya Hernandez",
"Credit Card", "Seattle");
INSERT INTO payment(customer_id, customer, mode, city) VALUES (104, "Liam Donovan",
"NetBanking", "Denver");
INSERT INTO payment(customer_id, customer, mode, city) VALUES (105, "Sophia Nguyen",
"Credit Card", "New Orleans");
INSERT INTO payment(customer_id, customer, mode, city) VALUES (106, "Caleb Foster",
"Debit Card", "Minneapolis");
INSERT INTO payment(customer_id, customer, mode, city) VALUES (107, "Ava Patel", "Debit
Card", "Phoenix");
INSERT INTO payment(customer_id, customer, mode, city) VALUES (108, "Lucas Carter",
"NetBanking", "Boston");
INSERT INTO payment(customer_id, customer, mode, city) VALUES (109, "Isabella Martinez",
"NetBanking", "Nashville");
INSERT INTO payment(customer_id, customer, mode, city) VALUES (110, "Jackson Brooks",
"Credit Card", "Boston");

-- Queries
SELECT * FROM payment;
SELECT mode, COUNT(mode) FROM payment GROUP BY mode ORDER BY COUNT(mode) DESC;
SELECT mode, COUNT(mode) FROM payment GROUP BY mode HAVING COUNT(mode) > 2 ORDER BY
COUNT(mode) DESC;
-- Clause order: SELECT -> FROM -> WHERE -> GROUP BY -> HAVING -> ORDER BY

You might also like