Primary Key
Primary Key
Primary Key
PRIMARY KEY
• Deletes all the rows where the sname is ‘Ramesh’ keeps all the other rows.
iii) Update Statement: It is used to make changes to existing rows of the table.
Syntax: UPDATE table_name SET column_name1 = value1, column_name2 = value2, …..
[WHERE Condition] Example:
INSERT INTO Employees (emp_id, first_name, last_name, date_of_birth, department, job_title, salary)
VALUES
(1, 'John', 'Doe', '1985-05-10', 'Human Resources', 'HR Manager', 65000.00),
(2, 'Jane', 'Smith', '1990-09-20', 'Marketing', 'Marketing Coordinator', 55000.00),
(3, 'Michael', 'Johnson', '1988-03-15', 'Finance', 'Financial Analyst', 70000.00),
(4, 'Emily', 'Brown', '1992-07-25', 'Sales', 'Sales Representative', 60000.00),
(5, 'William', 'Davis', '1987-11-05', 'IT', 'Systems Administrator', 75000.00),
(6, 'Emma', 'Wilson', '1995-01-30', 'Engineering', 'Software Engineer', 80000.00),
(7, 'Daniel', 'Martinez', '1989-12-12', 'Operations', 'Operations Manager', 72000.00),
(8, 'Olivia', 'Taylor', '1993-04-18', 'Customer Service', 'Customer Service Representative',
50000.00),
(9, 'Alexander', 'Anderson', '1986-08-08', 'Research and Development', 'Research Scientist',
85000.00),
(10, 'Sophia', 'Thomas', '1991-06-28', 'Product Management', 'Product Manager', 90000.00);
1) Display all employees
2) Display employees in a specific department (e.g., IT)
3) Display employees earning more than a certain salary (e.g., $60,000):
4) Update the job title of an employee (e.g., change job title of employee with emp_id = 3 to 'Senior
Financial Analyst')
5) Update the salary of an employee (e.g., increase salary of employee with emp_id = 5 by $5000)
6) Delete an employee from the table (e.g., delete employee with emp_id = 7)
7) Display employees born after a certain date (e.g., after January 1, 1990)
8) Display employees whose first name starts with a certain letter (e.g., 'J')
9) Update the department of an employee (e.g., change department of employee with emp_id = 4 to
'Customer Relations')
10) Delete employees based on certain criteria (e.g., delete employees whose salary is less than $50,000)
11) Display employees ordered by their salary in descending order:
12) Display the count of employees in each department
i)Entity Integrity Constraint: Primary Key Value cannot be NULL and duplicate.
INSERT INTO STUDENT VALUES (NULL,'CHETHAN','CSE','2000-20-03',4);
SELECT Command:
INSERT INTO Employees (emp_id, first_name, last_name, date_of_birth, department, job_title, salary)
VALUES
(1, 'John', 'Doe', '1985-05-10', 'Human Resources', 'HR Manager', 65000.00),
(2, 'Jane', 'Smith', '1990-09-20', 'Marketing', 'Marketing Coordinator', 55000.00),
(3, 'Michael', 'Johnson', '1988-03-15', 'Finance', 'Financial Analyst', 70000.00),
(4, 'Emily', 'Brown', '1992-07-25', 'Sales', 'Sales Representative', 60000.00),
(5, 'William', 'Davis', '1987-11-05', 'IT', 'Systems Administrator', 75000.00),
(6, 'Emma', 'Wilson', '1995-01-30', 'Engineering', 'Software Engineer', 80000.00),
(7, 'Daniel', 'Martinez', '1989-12-12', 'Operations', 'Operations Manager', 72000.00),
(8, 'Olivia', 'Taylor', '1993-04-18', 'Customer Service', 'Customer Service Representative',
50000.00),
(9, 'Alexander', 'Anderson', '1986-08-08', 'Research and Development', 'Research Scientist',
85000.00),
(10, 'Sophia', 'Thomas', '1991-06-28', 'Product Management', 'Product Manager', 90000.00);
SELECT emp_id, salary FROM employees WHERE salary BETWEEN 65000 AND 75000;
SELECT emp_id, first_name, last_name, dept_name FROM employees WHERE first_name LIKE
"%M";
SELECT emp_id, first_name, last_name, dept_name FROM employees WHERE first_name LIKE "J
%";
SELECT * FROM employees WHERE first_name LIKE '_o__';
This query will select all rows from the employees table where the 'first_name' column has exactly
four characters and the second character is 'o'.
Remember, in SQL, the underscore character (_) matches any single character, and the percent sign
(%) matches any sequence of characters.
CREATE TABLE Students (
student_id INT PRIMARY KEY, first_name VARCHAR(50),
last_name VARCHAR(50), date_of_birth DATE, address VARCHAR(100),
city VARCHAR(50), state VARCHAR(50), country VARCHAR(50),
email VARCHAR(100), marks INT );
SELECT ASCII('a');
SELECT ASCII('A');
SELECT ASCII('1');
SELECT ASCII('ABC');
SELECT LOWER('STRING FUNCTION');
SELECT UPPER(first_name) from students;
SELECT LENGTH('STRING FUNCTION');
SELECT LENGTH(first_name) from students;
SELECT LEFT('STRING FUNCTION', 6);
SELECT RIGHT('STRING FUNCTION', 6);
SELECT REVERSE('STRING FUNCTION');
SELECT REVERSE (first_name) from students;
SELECT SUBSTRING('STRING FUNCTION', 1, 6);
SELECT SUBSTRING('STRING FUNCTION', 8, 8);
It starts extracting from the 8th position ('F') and continues for a length of 8 characters,
resulting in 'FUNCTION'.
SELECT NOW();
SELECT SYSDATE();
Select first_name, state From Students Where state not in ('ny','ca','tx');
Select first_name, state From Students Where state in ('ny','ca','tx');
Select first_name, last_name From Students Where marks IS NULL;
Select first_name, last_name From Students Where marks IS NOT NULL;
SELECT state, AVG(marks) AS average_marks
FROM Students
GROUP BY state;
SELECT *
FROM Students
ORDER BY marks DESC;
3. Retrieve a list of all bank branch details, ordered by branch city, with each city’s branches
listed in reverse order of assets
SELECT BR_NAME, BR_CITY, ASSETS FROM BRANCH ORDER BY BR_CITY,
ASSETS DESC;
14. Find the branch name and balance of a customer kavya with account number 123456
20. Find all cities with more than two customers living in the city
21. Find all the customers who have at least two accounts at the main branch.
22. Demonstrate how you delete all account tuples at every branch located in a specific city.
23. Find all the customers who have an account at all the branches located in a specific city.
24. Find all the customers with more than one loan
25. Find branches with assets greater than all branches in Bangalore
1. SELECT * FROM ACCOUNT WHERE BALANCE > 20000;
2. SELECT * FROM ACCOUNT ORDER BY BALANCE;
3. SELECT BR_NAME, BR_CITY, ASSETS FROM BRANCH ORDER BY BR_CITY,
ASSETS DESC;
4. SELECT BR_NAME, BR_CITY, ASSETS FROM BRANCH ORDER BY BR_CITY,
ASSETS DESC;
5. SELECT COUNT(DISTINCT BR_NAME) FROM LOAN;
6. SELECT COUNT(*) FROM BRANCH WHERE BR_NAME NOT IN (SELECT BR_NAME
FROM LOAN);
7. SELECT BR_NAME FROM BRANCH WHERE BR_CITY = 'BENGALURU';
8. SELECT BR_NAME, COUNT(*) AS ACCOUNT_COUNT FROM ACCOUNT GROUP BY
BR_NAME;
9. SELECT BR_NAME, SUM(BALANCE) AS TOTAL_BALANCE FROM ACCOUNT
GROUP BY BR_NAME;
10. SELECT BR_NAME, SUM(AMOUNT) AS TOTAL_LOAN_BALANCE FROM LOAN
GROUP BY BR_NAME;
11. SELECT CUST_CITY FROM CUSTOMER WHERE CUST_NAME = (SELECT
CUST_NAME FROM DEPOSITOR WHERE ACCNO = 123456);
12. SELECT BR_NAME FROM BRANCH WHERE BR_NAME NOT IN (SELECT DISTINCT
BR_NAME FROM ACCOUNT);
13. SELECT SUM(AMOUNT) AS TOTAL_LOAN_AMOUNT FROM LOAN WHERE
LOAN_NO IN (SELECT LOAN_NO FROM BORROWER WHERE CUST_NAME =
'ABHAY');
14. SELECT BR_NAME, BALANCE
FROM ACCOUNT
WHERE ACCNO = 123456
AND BR_NAME = (SELECT BR_NAME FROM DEPOSITOR WHERE CUST_NAME =
'KAVYA' AND ACCNO = 123456);
15. SELECT CUST_NAME, SUM(AMOUNT) AS TOTAL_LOAN_AMOUNT
FROM LOAN
WHERE LOAN_NO IN (SELECT LOAN_NO FROM BORROWER WHERE
CUST_NAME = 'KAVYA' OR CUST_NAME = 'ABHAY')
GROUP BY CUST_NAME;
16. SELECT L.* FROM LOAN L JOIN BORROWER B ON L.LOAN_NO = B.LOAN_NO
WHERE B.CUST_NAME = 'KAVYA';
17. SELECT BR_CITY FROM BRANCH WHERE BR_NAME = (SELECT BR_NAME FROM
LOAN WHERE LOAN_NO = 100);
18. SELECT CUST_NAME, COUNT(*) AS ACCOUNT_COUNT FROM DEPOSITOR
GROUP BY CUST_NAME;
19. SELECT CUST_NAME FROM CUSTOMER WHERE CUST_NAME IN (SELECT
CUST_NAME FROM DEPOSITOR) AND CUST_NAME NOT IN (SELECT
CUST_NAME FROM BORROWER);
20. SELECT CUST_CITY FROM CUSTOMER GROUP BY CUST_CITY HAVING COUNT(*)
> 2;
21. SELECT CUST_NAME FROM DEPOSITOR WHERE BR_NAME = 'MAIN' GROUP BY
CUST_NAME HAVING COUNT(*) >= 2;
22. DELETE FROM ACCOUNT WHERE BR_NAME IN (SELECT BR_NAME FROM
BRANCH WHERE BR_CITY = 'CITY_NAME');
23. SELECT CUST_NAME
FROM DEPOSITOR
WHERE BR_NAME IN (SELECT BR_NAME FROM BRANCH WHERE BR_CITY =
'CITY_NAME')
GROUP BY CUST_NAME
HAVING COUNT(DISTINCT BR_NAME) = (SELECT COUNT(DISTINCT BR_NAME)
FROM BRANCH WHERE BR_CITY = 'CITY_NAME');
UNION: This operation combines the results of two or more SELECT statements into a
single result set. It removes duplicate rows by default.
Example:
SELECT column1 FROM table1
UNION
SELECT column1 FROM table2;
INTERSECT: This operation returns only the rows that appear in both result sets of two
SELECT statements.
Example:
SELECT column1 FROM table1
INTERSECT
SELECT column1 FROM table2;
Find the cities that appear in both the branch and customer tables.
SELECT BR_CITY FROM BRANCH
INTERSECT
SELECT CUST_CITY FROM CUSTOMER;
EXCEPT (or MINUS in some databases): This operation returns the rows that are present in
the first result set but not in the second result set.
Example:
SELECT column1 FROM table1
EXCEPT
SELECT column1 FROM table2;
Find the cities that are in the branch table but not in the customer table
SELECT BR_CITY FROM BRANCH
EXCEPT
SELECT CUST_CITY FROM CUSTOMER;
SELECT BR_NAME
FROM BRANCH
WHERE BR_NAME NOT IN (SELECT BR_NAME FROM LOAN);
CARTESIAN PRODUCT:
SELECT * FROM BRANCH CROSS JOIN CUSTOMER;
SELECT *
FROM EMPLOYEE E
LEFT JOIN
DEPARTMENT D ON E.DEPT_NUM = D.DEPT_ID;
RIGHT JOIN
DEPARTMENT D
ON E.DEPT_NUM=D.DEPT_ID;
UNION
To study and implement Sub queries/Nested queries, Correlated nested queries in SQL.
CREATE TABLE Student (
sid INTEGER PRIMARY KEY,
sname VARCHAR(20),
major VARCHAR(20),
address VARCHAR(20),
level VARCHAR(20),
age INTEGER,
DOB DATE
);
INSERT INTO STUDENT (SID, SNAME, MAJOR, LEVEL, AGE, ADDRESS, DOB)
VALUES
(101, 'ABHI', 'CSE', 'JR', 19, 'BANGALORE', '1980-01-23'),
(102, 'ANIL', 'ISE', 'JR', 18, 'DAVANAGERE', '1984-02-12'),
(103, 'BHAVYA', 'ISE', 'SR', 20, 'CHITRADURGA', '1998-03-22'),
(104, 'CHETHAN', 'CSE', 'JR', 19, 'BELAGAVI', '1999-04-26'),
(105, 'SURESH', 'MECH', 'JR', 18, 'HUBLI', '1970-12-20'),
(106, 'JAYANTH', 'CSE', 'SR', 20, 'DHARVAD', '1988-11-16');
SELECT C.CNAME
FROM COURSE C
WHERE C.ROOM = 'KG04' OR C.CNAME IN (
SELECT E.CNAME
FROM ENROLLED E
GROUP BY E.CNAME
HAVING COUNT(*) >= 5
);
FROM Student S
WHERE S.sid IN (
SELECT E.sid
FROM Enrolled E
GROUP BY E.sid
HAVING COUNT(*) = (
SELECT MAX(enrollment_count)
FROM (
FROM Enrolled
GROUP BY sid
) AS max_enrollments
);