├── CODE_OF_CONDUCT.md ├── EXPT1.md ├── EXPT11.md ├── EXPT3.md ├── EXPT4.md ├── EXPT5.md ├── EXPT6.md ├── EXPT7.md ├── EXPT8.md ├── EXPT9.md ├── Expt10.md ├── Expt12.md ├── LICENSE └── README.md /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | . 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | -------------------------------------------------------------------------------- /EXPT1.md: -------------------------------------------------------------------------------- 1 | # 1. Create table 2 | ```SQL 3 | CREATE TABLE emp ( 4 | empno NUMBER, 5 | empname VARCHAR2(255), 6 | DOB DATE, 7 | salary NUMBER, 8 | designation VARCHAR2(20) 9 | ); 10 | ``` 11 | 12 | # 2. Insert values 13 | ```SQL 14 | INSERT INTO emp VALUES(100,'John','4.21.1994', 50000,'Manager'); 15 | ``` 16 | ```SQL 17 | INSERT INTO emp VALUES(101,'Greg','6.20.1994',25000,'Clerk'); 18 | ``` 19 | 20 | # 3. Display values 21 | ```SQL 22 | SELECT * FROM emp; 23 | ``` 24 | ```SQL 25 | SELECT empname,salary FROM emp; 26 | ``` -------------------------------------------------------------------------------- /EXPT11.md: -------------------------------------------------------------------------------- 1 | ``` 2 | Give this Repository a ⭐️⭐️ Star ⭐️⭐️ for updates. 3 | COPY PASTE ALL THE QUERIES ONE BY ONE IN SQLPLUS TO EXECUTE IT WITHOUT ANY ERROR 4 | ``` 5 | 6 | # PL/SQL Functions 7 | # ** Execute this command in sqlplus 1st ** 8 | ## TO display output in the screen – give the below command initially 9 | ```sql 10 | set serveroutput on; 11 | ``` 12 | 13 | ## 1) creates a simple procedure that displays the string Hello World!; on the screen when executed 14 | ```sql 15 | CREATE OR REPLACE PROCEDURE display_hello_world IS 16 | BEGIN 17 | DBMS_OUTPUT.PUT_LINE('Hello World!'); 18 | END; 19 | / 20 | ``` 21 | ```sql 22 | EXECUTE display_hello_world; 23 | ``` 24 | ## 2) Create a procedure to find the minimum of two values. HINT - Procedure takes two numbers using the IN mode and returns their minimum using the OUT parameters 25 | ```sql 26 | CREATE OR REPLACE PROCEDURE find_minimum ( 27 | num1 IN NUMBER, 28 | num2 IN NUMBER, 29 | min_num OUT NUMBER 30 | ) IS 31 | BEGIN 32 | IF num1 < num2 THEN 33 | min_num := num1; 34 | ELSE 35 | min_num := num2; 36 | END IF; 37 | END; 38 | / 39 | ``` 40 | ```sql 41 | DECLARE 42 | num1 NUMBER := 10; 43 | num2 NUMBER := 5; 44 | min_num NUMBER; 45 | BEGIN 46 | find_minimum(num1, num2, min_num); 47 | DBMS_OUTPUT.PUT_LINE('The minimum of ' || num1 || ' and ' || num2 || ' is ' || min_num); 48 | END; 49 | / 50 | ``` 51 | 52 | ## 3) Create a procedure, to get cube of passed number. 53 | ```sql 54 | CREATE OR REPLACE PROCEDURE get_cube ( 55 | num IN NUMBER, 56 | cube_num OUT NUMBER 57 | ) IS 58 | BEGIN 59 | cube_num := num * num * num; 60 | END; 61 | / 62 | ``` 63 | ```sql 64 | DECLARE 65 | num NUMBER := 5; 66 | cube NUMBER; 67 | BEGIN 68 | get_cube(num, cube); 69 | DBMS_OUTPUT.PUT_LINE('The cube of ' || num || ' is ' || cube); 70 | END; 71 | / 72 | ``` 73 | 74 | ## 4) Write a procedure to reverse a input string and check it is palindrome or not. 75 | ```sql 76 | CREATE OR REPLACE PROCEDURE check_palindrome(input_string IN VARCHAR2) IS 77 | reversed_string VARCHAR2(4000); 78 | BEGIN 79 | -- Reverse the input string 80 | reversed_string := REVERSE(input_string); 81 | 82 | -- Check if the reversed string is equal to the input string 83 | IF input_string = reversed_string THEN 84 | DBMS_OUTPUT.PUT_LINE('The input string is a palindrome.'); 85 | ELSE 86 | DBMS_OUTPUT.PUT_LINE('The input string is not a palindrome.'); 87 | END IF; 88 | END check_palindrome; 89 | / 90 | ``` 91 | ```sql 92 | BEGIN 93 | check_palindrome('level'); 94 | END; 95 | / 96 | ``` 97 | 98 | ## 5) Write a procedure to delete a specific row from the table already created. 99 | ``` sql 100 | CREATE TABLE student4 ( 101 | id NUMBER(10) PRIMARY KEY, 102 | name VARCHAR2(100) NOT NULL, 103 | email VARCHAR2(100) UNIQUE, 104 | phone VARCHAR2(20), 105 | age NUMBER(3), 106 | gender VARCHAR2(10), 107 | address VARCHAR2(200) 108 | ); 109 | ``` 110 | 111 | ``` sql 112 | INSERT INTO student4 (id, name, email, phone, age, gender, address) 113 | VALUES (101, 'John Smith', 'john.smith@example.com', '555-1234', 25, 'Male', '123 Main St'); 114 | 115 | INSERT INTO student4 (id, name, email, phone, age, gender, address) 116 | VALUES (202, 'Jane Doe', 'jane.doe@example.com', '555-5678', 22, 'Female', '456 Maple Ave'); 117 | 118 | INSERT INTO student4 (id, name, email, phone, age, gender, address) 119 | VALUES (303, 'Bob Johnson', 'bob.johnson@example.com', '555-2468', 28, 'Male', '789 Elm St'); 120 | ``` 121 | 122 | ``` sql 123 | CREATE OR REPLACE PROCEDURE delete_row( 124 | row_id IN NUMBER 125 | ) 126 | IS 127 | BEGIN 128 | DELETE FROM student4 WHERE id = row_id; 129 | COMMIT; 130 | END; 131 | / 132 | ``` 133 | 134 | ``` sql 135 | DECLARE 136 | row_id NUMBER := 101; 137 | BEGIN 138 | delete_row(row_id); 139 | DBMS_OUTPUT.PUT_LINE('Row with ID ' || row_id || ' deleted.'); 140 | END; 141 | / 142 | ``` 143 | -------------------------------------------------------------------------------- /EXPT3.md: -------------------------------------------------------------------------------- 1 | ## Table 2 | ```sql 3 | create table manager(enumber number(2),ename char(15),salary number(5),commission number(4),annualsalary number(7),Hiredate date,designation char(10),deptno number(2),reporting char(10)); 4 | ``` 5 | ## Values 6 | ```sql 7 | insert into manager values(7369,'Dharsan',2500,500,30000,'30-June-81','clerk',10,'John'); 8 | insert into manager values(7839,'Subu',3000,400,36000,'1-Jul-82','manager',null,'James'); 9 | insert into manager values(7934,'Aadhi',3500,300,42000,'1-May-82','manager',30,NULL); 10 | insert into manager values(7788,'Vikash',4000,0,48000,'12-Aug-82','clerk',50,'Bond'); 11 | ``` 12 | ## Q1) Update all the records of manager table by increasing 10% of their salary as bonus. 13 | ```sql 14 | UPDATE manager set salary = salary+salary/10; 15 | ``` 16 | ## Q2) Delete the records from manager table where the salary less than 2750. 17 | ```sql 18 | DELETE FROM manager WHERE salary < 2750; 19 | ``` 20 | ## Q3) Display each name of the employee as “Name” and annual salary as “Annual Salary” (Note: Salary in emp table is the monthly salary) 21 | ```sql 22 | SELECT ename as "Name",salary*12 as annualsalary from manager; 23 | ``` 24 | ## Q4) List concatenated value of name and designation of each employee. 25 | ```sql 26 | SELECT ename || ' ' || designation as concat FROM manager; 27 | ``` 28 | ## Q5) List the names of Clerks from emp table. 29 | ```sql 30 | select ename from manager where designation = 'clerk'; 31 | ``` 32 | ## Q6) List the Details of Employees who have joined before 30 Sept 81. 33 | ```SQL 34 | select * from manager where Hiredate < '30-sep-81'; 35 | ``` 36 | ## Q7) List names of employees who’s employee numbers are 7369,7839,7934,7788. 37 | ```sql 38 | select enumber from manager where enumber in (7369,7521,7839,7934,7788); 39 | ``` 40 | ## Q8) List the names of employee who are not Managers. 41 | ```sql 42 | select ename from manager where designation != 'manager'; 43 | ``` 44 | ## Q9) List the names of employees not belonging to dept no 30,40 & 10 45 | ```sql 46 | select ename from manager where deptno != 30 and deptno != 40 and deptno != 10 ; 47 | ``` 48 | ## Q10) List names of those employees joined between 30 June 81 and 31 Dec 81. 49 | ```sql 50 | select ename from manager where Hiredate between '30-Jun-81' and '31-Dec-81'; 51 | ``` 52 | ## Q11) List different designations in the company. 53 | ```sql 54 | select distinct designation from manager; 55 | ``` 56 | ## Q12) List the names of employees not eligible for commission. 57 | ```sql 58 | select ename from manager where commission =0; 59 | ``` 60 | ## Q13) List names and designations of employee who does not report to anybody 61 | ```sql 62 | select ename,designation from manager where reporting is NULL; 63 | ``` 64 | ## Q14) List all employees not assigned to any department. 65 | ```sql 66 | select ename from manager where deptno is NULL; 67 | ``` 68 | ## Q15) List names of employee who are eligible for commission. 69 | ```sql 70 | select ename from manager where commission > 0; 71 | ``` 72 | ## Q16) List employees whose name either start or end with ‘s’. 73 | ```sql 74 | select ename from manager where ename like 'S%' or ename like '%S'; 75 | ``` 76 | ## Q17) List names of employees whose names have ‘i’ as the second character. 77 | ```sql 78 | select ename from manager where ename like '_i%'; 79 | ``` 80 | ## Q18) Sort emp table in ascending order by hire-date and list ename, job, deptno and hire-date. 81 | ```sql 82 | select Hiredate,ename,designation,deptno from manager order by Hiredate; 83 | ``` 84 | ## Q19) Sort emp table in descending order by annual salary and list empno, ename, job and annual-salary. 85 | ```sql 86 | select enumber,ename,designation,annualsalary from manager order by annualsalary desc; 87 | ``` 88 | ## Q20) List ename, deptno and sal after sorting emp table in ascending order by deptno and then descending order by sal. 89 | ```sql 90 | select ename,deptno,salary from manager order by deptno, salary desc; 91 | ``` -------------------------------------------------------------------------------- /EXPT4.md: -------------------------------------------------------------------------------- 1 | # INTEGRITY CONSTRAINTS 2 | ## Q1) 3 | 4 | ```sql 5 | CREATE TABLE DEPT ( 6 | DEPT_NO NUMBER(5) PRIMARY KEY, 7 | DNAME VARCHAR(10), 8 | DLOC VARCHAR(10) 9 | ); 10 | ``` 11 | 12 | ## Q2) 13 | 14 | ```sql 15 | CREATE TABLE EMP ( 16 | EMP_NO NUMBER(5) CONSTRAINT PK_EMP PRIMARY KEY, 17 | ENAME VARCHAR(10), 18 | JOB VARCHAR(10), 19 | MANAGER_NAME VARCHAR(10) DEFAULT 'Mr.K. RAM', 20 | HIRE_DATE DATE, 21 | SALARY NUMBER(30), 22 | COMMISSION NUMBER(30), 23 | DEPT_NO NUMBER(5) 24 | ); 25 | ``` 26 | 27 | # Q3) 28 | 29 | ```sql 30 | ALTER TABLE DEPT ADD CONSTRAINT UQ_DNAME UNIQUE (DNAME); 31 | ``` 32 | 33 | ## Q4) 34 | 35 | ```sql 36 | ALTER TABLE EMP MODIFY HIRE_DATE DATE NOT NULL; 37 | ``` 38 | 39 | ## Q5) 40 | 41 | ```sql 42 | ALTER TABLE EMP ADD CONSTRAINT CHK_SALARY_RANGE CHECK (SALARY BETWEEN 10000 AND 20000); 43 | ``` 44 | 45 | ## Q6) 46 | 47 | ```sql 48 | ALTER TABLE EMP ADD CONSTRAINT FK_DEPTNO_DEPT FOREIGN KEY (DEPT_NO) REFERENCES DEPT(DEPT_NO); 49 | ``` 50 | 51 | ## Q7) 52 | 53 | ```sql 54 | ALTER TABLE EMP ADD CONSTRAINT CHK_COMMISSION CHECK (COMMISSION < 0.1 * SALARY); 55 | ``` 56 | 57 | ## Q8) 58 | 59 | ```sql 60 | INSERT INTO DEPT (DEPT_NO, DNAME, DLOC) VALUES (10, 'ACCOUNTING', 'NEW YORK'); 61 | ``` 62 | 63 | ```sql 64 | INSERT INTO EMP (EMP_NO, ENAME, JOB, HIRE_DATE, SALARY, COMMISSION, DEPT_NO) VALUES (7369, 'SMITH', 'CLERK', '17-DEC-80', 11800, NULL, 10); 65 | ``` 66 | 67 | ## Q9) 68 | 69 | ```sql 70 | INSERT INTO EMP (EMP_NO, ENAME, JOB, HIRE_DATE, SALARY, COMMISSION, DEPT_NO) VALUES (7499, 'ALLEN', 'SALESMAN', '20-FEB-81', 11600, NULL, 30); 71 | ``` 72 | # OUTPUT 73 | ``` 74 | Error: FOREIGN KEY constraint failed 75 | ``` 76 | 77 | ## Q10) 78 | 79 | ```sql 80 | ALTER TABLE EMP DROP CONSTRAINT PK_EMP; 81 | ``` 82 | -------------------------------------------------------------------------------- /EXPT5.md: -------------------------------------------------------------------------------- 1 | ``` 2 | Give this Repository a ⭐️⭐️ Star ⭐️⭐️ for updates. 3 | COPY PASTE ALL THE QUERIES ONE BY ONE IN SQLPLUS TO EXECUTE IT WITHOUT ANY ERROR 4 | ``` 5 | # DCL 6 | ## Q1 Give grant permission to your neighbor for any one of your tables. Tell him/her to access (modify the data) your table from their login. 7 | ```SQL 8 | GRANT SELECT, INSERT, UPDATE ON DEPT TO RA2011003010737; 9 | ``` 10 | 11 | ## Q2 Check the table again from your login. Observe the inference. 12 | ``` 13 | DESC DEPT; 14 | ``` 15 | 16 | ## Q3 Revoke the permission and tell them to try for accessing your table. 17 | ``` 18 | REVOKE SELECT, INSERT, UPDATE ON DEPT FROM RA2011003010737; 19 | ``` 20 | 21 | # TCL 22 | ## Q1 Create the department table add the details and commit the data. 23 | 24 | ```sql 25 | CREATE TABLE DEPT1 (DEPTNO INT PRIMARY KEY, DNAME VARCHAR(20), LOC VARCHAR(20)); 26 | ``` 27 | ```sql 28 | INSERT INTO DEPT1 (DEPTNO, DNAME, LOC) VALUES (10, 'ACCOUNTING', 'NEW YORK'); 29 | ``` 30 | ```sql 31 | INSERT INTO DEPT1 (DEPTNO, DNAME, LOC) VALUES (20, 'RESEARCH', 'DALLAS'); 32 | ``` 33 | ```sql 34 | INSERT INTO DEPT1 (DEPTNO, DNAME, LOC) VALUES (30, 'SALES', 'CHICAGO'); 35 | ``` 36 | ```sql 37 | INSERT INTO DEPT1 (DEPTNO, DNAME, LOC) VALUES (40, 'OPERATIONS', 'BOSTON'); 38 | ``` 39 | ```sql 40 | COMMIT; 41 | ``` 42 | 43 | ## Q2 Update the location of dept number ‘40’ as ‘San Francisco’ and don’t commit the table. 44 | ```sql 45 | UPDATE DEPT1 SET LOC = 'SAN FRANCISCO' WHERE DEPTNO = 40; 46 | ``` 47 | 48 | ## Q3 Rollback to the previous state of data in the deparment table and specify the output of the table with select query. 49 | ```SQL 50 | ROLLBACK; 51 | ``` 52 | ```SQL 53 | SELECT * FROM DEPT1; 54 | ``` 55 | 56 | ## Q4 Delete all the ENTRIES from department table from the location CHICAGO. 57 | ```SQL 58 | DELETE FROM DEPT1 WHERE LOC = 'CHICAGO'; 59 | ``` 60 | 61 | ## Q5 Change LOC=’BOSTON’ for deptno=40 in DEPT table 62 | ```SQL 63 | UPDATE DEPT1 SET LOC = 'BOSTON' WHERE DEPTNO = 40; 64 | ``` 65 | 66 | ## Q6 Create SAVEPOINT in the name ‘update_over’ 67 | ```SQL 68 | SAVEPOINT update_over; 69 | ``` 70 | 71 | ## Q7 Insert another row in DEPT table with your own values 72 | ```SQL 73 | INSERT INTO DEPT1 (DEPTNO, DNAME, LOC) VALUES (50, 'MARKETING', 'LOS ANGELES'); 74 | ``` 75 | 76 | ## Q8 Display the updated data as of now in the department table. 77 | ```SQL 78 | SELECT * FROM DEPT1; 79 | ``` 80 | 81 | ## Q9 Create SAVEPOINT in the name ‘update_another’ 82 | ```SQL 83 | SAVEPOINT update_another; 84 | ``` 85 | 86 | ## Q10Display the data. Then Rollback the transaction upto the point ‘update_over’ and display the details of table and write the inference. 87 | ```SQL 88 | SELECT * FROM DEPT1; 89 | ``` 90 | ```SQL 91 | ROLLBACK TO update_over; 92 | ``` 93 | ```SQL 94 | SELECT * FROM DEPT1; 95 | ``` 96 | -------------------------------------------------------------------------------- /EXPT6.md: -------------------------------------------------------------------------------- 1 | ``` 2 | Give this Repository a ⭐️⭐️ Star ⭐️⭐️ for updates. 3 | COPY PASTE ALL THE QUERIES ONE BY ONE IN SQLPLUS TO EXECUTE IT WITHOUT ANY ERROR 4 | ``` 5 | # SQL FUNCTIONS 6 | ## Drop the existing table 7 | ```sql 8 | Drop table emp; 9 | ``` 10 | ## Create The table emp 11 | ```sql 12 | CREATE TABLE emp (EMPNO NUMBER(4) PRIMARY KEY,ENAME VARCHAR2(10),JOB VARCHAR2(9),MGR NUMBER(4),HIREDATE DATE,SAL NUMBER(7,2),COMM NUMBER(7,2),DEPTNO NUMBER(2)); 13 | ``` 14 | ## Insert Values 15 | ```sql 16 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 17 | VALUES (7369, 'SMITH', 'CLERK', 7902, '17-DEC-80', 800, NULL, 20); 18 | 19 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 20 | VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '20-FEB-81', 1600, 300, 30); 21 | 22 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 23 | VALUES (7521, 'WARD', 'SALESMAN', 7698, '22-FEB-81', 1250, 500, 30); 24 | 25 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 26 | VALUES (7566, 'JONES', 'MANAGER', 7839, '02-APR-81', 2975, NULL, 20); 27 | 28 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 29 | VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '28-SEP-81', 1250, 1400, 30); 30 | 31 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 32 | VALUES (7698, 'BLAKE', 'MANAGER', 7839, '01-MAY-81', 2850, NULL, 30); 33 | 34 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 35 | VALUES (7782, 'CLARK', 'MANAGER', 7839, '09-JUN-81', 2450, NULL, 10); 36 | 37 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 38 | VALUES (7788, 'SCOTT', 'ANALYST', 7566, '19-APR-87', 3000, NULL, 20); 39 | 40 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 41 | VALUES (7839, 'KING', 'PRESIDENT', NULL, '17-NOV-81', 5000, NULL, 10); 42 | 43 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 44 | VALUES (7844, 'TURNER', 'SALESMAN', 7698, '08-SEP-81', 1500, 0, 30); 45 | 46 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 47 | VALUES (7876, 'ADAMS', 'CLERK', 7788, '23-MAY-87', 1100, NULL, 20); 48 | 49 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 50 | VALUES (7900, 'JAMES', 'CLERK', 7698, '03-DEC-81', 950, NULL, 30); 51 | 52 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 53 | VALUES (7902, 'FORD', 'ANALYST', 7566, TO_DATE('03-DEC-81', 'DD-MON-RR'), 3000, 20, 20); 54 | 55 | INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 56 | VALUES (7934, 'MILLER', 'CLERK', 7782, TO_DATE('23-JAN-82', 'DD-MON-RR'), 1300, 10, 10); 57 | ``` 58 | 59 | ## Q1) Find number of rows in the table EMP 60 | ```sql 61 | SELECT COUNT(*) FROM emp; 62 | ``` 63 | 64 | ## Q2) Find number of designations available in EMP table. 65 | ```sql 66 | SELECT COUNT(DISTINCT job) FROM emp; 67 | ``` 68 | 69 | ## Q3) What is the difference between the following queries 70 | ```sql 71 | select count(comm) from emp; 72 | ``` 73 | ```sql 74 | select count(nvl(comm,0)) from emp; 75 | ``` 76 | 77 | ## Q4) Find maximum, minimum and average salary in EMP table. 78 | ```sql 79 | SELECT MAX(sal), MIN(sal), AVG(sal) FROM emp; 80 | ``` 81 | 82 | ## Q5) Find number of employees who work in department number 30 83 | ```sql 84 | SELECT COUNT(*) FROM emp WHERE deptno = 30; 85 | ``` 86 | 87 | ## Q6) Find the maximum salary paid to a ‘CLERK’ 88 | ```sql 89 | SELECT MAX(sal) FROM emp WHERE job = 'CLERK'; 90 | ``` 91 | 92 | ## Q7) List the jobs and number of employees in each job. The result should be in the descending order of the number of employees. 93 | ```sql 94 | SELECT job, COUNT(*) as num_employees FROM emp GROUP BY job ORDER BY num_employees DESC; 95 | ``` 96 | 97 | ## Q8) List the total salary, maximum and minimum salary and average salary of the employees jobwise. 98 | ```sql 99 | SELECT job, SUM(sal) as total_salary, MAX(sal) as max_salary, MIN(sal) as min_salary, AVG(sal) as avg_salary FROM emp GROUP BY job; 100 | ``` 101 | 102 | ## Q9) List the total salary, maximum and minimum salary and average salary of the employees jobwise, for department 20 and display only those rows having an average salary > 1000. 103 | ```sql 104 | SELECT job, SUM(sal) as total_salary, MAX(sal) as max_salary, MIN(sal) as min_salary, AVG(sal) as avg_salary FROM emp WHERE deptno = 20 GROUP BY job HAVING AVG(sal) > 1000; 105 | ``` 106 | 107 | ## Q10) List the job and total salary of employees jobwise, for jobs other than ‘PRESIDENT’ and display only those rows having total salary > 5000. 108 | ```sql 109 | SELECT job, SUM(sal) as total_salary FROM emp WHERE job != 'PRESIDENT' GROUP BY job HAVING SUM(sal) > 5000; 110 | ``` -------------------------------------------------------------------------------- /EXPT7.md: -------------------------------------------------------------------------------- 1 | ``` 2 | Give this Repository a ⭐️⭐️ Star ⭐️⭐️ for updates. 3 | COPY PASTE ALL THE QUERIES ONE BY ONE IN SQLPLUS TO EXECUTE IT WITHOUT ANY ERROR 4 | ``` 5 | 6 | # JOINING TABLES 7 | ## Create a Customer1 Table 8 | ```sql 9 | CREATE TABLE Customer1 (customer_id INT,cust_name VARCHAR(50),city VARCHAR(50),grade INT,salesman_id INT); 10 | ``` 11 | 12 | ## Inserting Values to the Table 13 | ```sql 14 | INSERT INTO Customer1 (customer_id, cust_name, city, grade, salesman_id) VALUES(3002, 'Nick Rimando', 'New York', 100, 5001); 15 | INSERT INTO Customer1 (customer_id, cust_name, city, grade, salesman_id) VALUES(3007, 'Brad Davis', 'New York', 200, 5001); 16 | INSERT INTO Customer1 (customer_id, cust_name, city, grade, salesman_id) VALUES(3005, 'Graham Zusi', 'California', 200, 5002); 17 | INSERT INTO Customer1 (customer_id, cust_name, city, grade, salesman_id) VALUES(3008, 'Julian Green', 'London', 300, 5002); 18 | INSERT INTO Customer1 (customer_id, cust_name, city, grade, salesman_id) VALUES(3004, 'Fabian Johnson', 'Paris', 300, 5006); 19 | INSERT INTO Customer1 (customer_id, cust_name, city, grade, salesman_id) VALUES(3009, 'Geoff Cameron', 'Berlin', 100, 5003); 20 | INSERT INTO Customer1 (customer_id, cust_name, city, grade, salesman_id) VALUES(3003, 'Jozy Altidor', 'Moscow', 200, 5007); 21 | INSERT INTO Customer1 (customer_id, cust_name, city, grade, salesman_id) VALUES(3001, 'Brad Guzan', 'London', NULL, 5005); 22 | ``` 23 | 24 | ## Create a Salesperson1 table 25 | ```sql 26 | CREATE TABLE Salesman1 (salesman_id INT,name VARCHAR(50),city VARCHAR(50),commission DECIMAL(4,2)); 27 | ``` 28 | 29 | ## Inserting Values to the Table 30 | ```sql 31 | INSERT INTO Salesman1 (salesman_id, name, city, commission) VALUES(5001, 'James Hoog', 'New York', 0.15); 32 | INSERT INTO Salesman1 (salesman_id, name, city, commission) VALUES(5002, 'Nail Knite', 'Paris', 0.13); 33 | INSERT INTO Salesman1 (salesman_id, name, city, commission) VALUES(5005, 'Pit Alex', 'London', 0.11); 34 | INSERT INTO Salesman1 (salesman_id, name, city, commission) VALUES(5006, 'Mc Lyon', 'Paris', 0.14); 35 | INSERT INTO Salesman1 (salesman_id, name, city, commission) VALUES(5007, 'Paul Adam', 'Rome', 0.13); 36 | INSERT INTO Salesman1 (salesman_id, name, city, commission) VALUES(5003, 'Lauson Hen', 'San Jose', 0.12); 37 | ``` 38 | 39 | ## Create a Order1 table 40 | ```sql 41 | CREATE TABLE Order1 (ord_no INT,purch_amt DECIMAL(8,2),ord_date DATE,customer_id INT,salesman_id INT); 42 | ``` 43 | 44 | ```sql 45 | ALTER SESSION SET nls_date_format='yyyy-mm-dd'; 46 | ``` 47 | 48 | ## Inserting Values to the Table 49 | ```sql 50 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70001, 150.5, '2012-10-05', 3005, 5002); 51 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70009, 270.65, '2012-09-10', 3001, 5005); 52 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70002, 65.26, '2012-10-05', 3002, 5001); 53 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70004, 110.5, '2012-08-17', 3009, 5003); 54 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70007, 948.5, '2012-09-10', 3005, 5002); 55 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70005, 2400.6, '2012-07-27', 3007, 5001); 56 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70008, 5760, '2012-09-10', 3002, 5001); 57 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70010, 1983.43, '2012-10-10', 3004, 5006); 58 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70003, 2480.4, '2012-10-10', 3009, 5003); 59 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70012, 250.45, '2012-06-27', 3008, 5002); 60 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70011, 75.29, '2012-08-17', 3003, 5007); 61 | INSERT INTO Order1 (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES (70013, 3045.6, '2012-04-25', 3002, 5001); 62 | ``` 63 | ## Q1) write a SQL query to find the salesperson and customer who reside in the same city. Return Salesman, cust_name and city. 64 | ```sql 65 | SELECT s.name AS Salesman1, c.cust_name, c.city FROM Salesman1 s INNER JOIN Customer1 c ON s.city = c.city; 66 | ``` 67 | 68 | ## Q2) write a SQL query to find the salesperson(s) and the customer(s) he represents. Return Customer Name, city, Salesman, commission. 69 | ```sql 70 | SELECT c.cust_name AS Customer_Name, c.city, s.name AS Salesman, s.commission FROM Customer1 c INNER JOIN Salesman1 s ON c.salesman_id = s.salesman_id; 71 | ``` 72 | 73 | ## Q3) Write a SQL statement to make a Cartesian product between salesman and customer 74 | ```sql 75 | SELECT * FROM Salesman1, Customer1; 76 | ``` 77 | 78 | ## Q4) Write a SQL statement to generate a list in ascending order of salespersons who work either for one or more customers or have not yet joined any of the customers. 79 | ```sql 80 | SELECT s.name AS Salesman FROM Salesman1 s LEFT JOIN Customer1 c ON s.salesman_id = c.salesman_id WHERE c.salesman_id IS NOT NULL OR c.salesman_id IS NULL ORDER BY s.name ASC; 81 | ``` 82 | 83 | ## Q5) Write a SQL query to find salespeople who received commissions of more than 10 percent from the company. Return Customer Name, customer city, Salesman, commission. 84 | ```sql 85 | SELECT c.cust_name AS Customer_Name, c.city AS Customer_City, s.name AS Salesman, s.commission FROM Customer1 c JOIN Salesman1 s ON c.salesman_id = s.salesman_id WHERE s.commission > 0.1 ORDER BY s.name ASC; 86 | ``` -------------------------------------------------------------------------------- /EXPT8.md: -------------------------------------------------------------------------------- 1 | ``` 2 | Give this Repository a ⭐️⭐️ Star ⭐️⭐️ for updates. 3 | COPY PASTE ALL THE QUERIES ONE BY ONE IN SQLPLUS TO EXECUTE IT WITHOUT ANY ERROR 4 | ``` 5 | # S U B Q U E R I E S 6 | ## Drop the existing table 7 | ```sql 8 | Drop table EMP; 9 | ``` 10 | ```sql 11 | Drop table DEPT; 12 | ``` 13 | ## Create The table EMP 14 | ```sql 15 | CREATE TABLE EMP (EMPNO NUMBER(4) PRIMARY KEY,ENAME VARCHAR2(10),JOB VARCHAR2(9),MGR NUMBER(4),HIREDATE DATE,SAL NUMBER(7,2),COMM NUMBER(7,2),DEPTNO NUMBER(2)); 16 | ``` 17 | ## Insert Values 18 | ```sql 19 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 20 | VALUES (7369, 'SMITH', 'CLERK', 7902, '17-DEC-80', 800, NULL, 20); 21 | 22 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 23 | VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '20-FEB-81', 1600, 300, 30); 24 | 25 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 26 | VALUES (7521, 'WARD', 'SALESMAN', 7698, '22-FEB-81', 1250, 500, 30); 27 | 28 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 29 | VALUES (7566, 'JONES', 'MANAGER', 7839, '02-APR-81', 2975, NULL, 20); 30 | 31 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 32 | VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '28-SEP-81', 1250, 1400, 30); 33 | 34 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 35 | VALUES (7698, 'BLAKE', 'MANAGER', 7839, '01-MAY-81', 2850, NULL, 30); 36 | 37 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 38 | VALUES (7782, 'CLARK', 'MANAGER', 7839, '09-JUN-81', 2450, NULL, 10); 39 | 40 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 41 | VALUES (7788, 'SCOTT', 'ANALYST', 7566, '19-APR-87', 3000, NULL, 20); 42 | 43 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 44 | VALUES (7839, 'KING', 'PRESIDENT', NULL, '17-NOV-81', 5000, NULL, 10); 45 | 46 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 47 | VALUES (7844, 'TURNER', 'SALESMAN', 7698, '08-SEP-81', 1500, 0, 30); 48 | 49 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 50 | VALUES (7876, 'ADAMS', 'CLERK', 7788, '23-MAY-87', 1100, NULL, 20); 51 | 52 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 53 | VALUES (7900, 'JAMES', 'CLERK', 7698, '03-DEC-81', 950, NULL, 30); 54 | 55 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 56 | VALUES (7902, 'FORD', 'ANALYST', 7566, TO_DATE('03-DEC-81', 'DD-MON-RR'), 3000, 20, 20); 57 | 58 | INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 59 | VALUES (7934, 'MILLER', 'CLERK', 7782, TO_DATE('23-JAN-82', 'DD-MON-RR'), 1300, 10, 10); 60 | ``` 61 | ## Create the Table dept 62 | ```sql 63 | CREATE TABLE DEPT (DEPTNO NUMBER(2) PRIMARY KEY,DNAME VARCHAR2(14),LOC VARCHAR2(13)); 64 | ``` 65 | 66 | ## Insert Values to the Table 67 | ```sql 68 | INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES (10, 'ACCOUNTING', 'NEW YORK'); 69 | 70 | INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES (20, 'RESEARCH', 'DALLAS'); 71 | 72 | INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES (30, 'SALES', 'CHICAGO'); 73 | 74 | INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES (40, 'OPERATIONS', 'BOSTON'); 75 | ``` 76 | 77 | ## Q1) List the name of the employees whose salary is greater than that of employee with empno 7566. 78 | ```sql 79 | SELECT ENAME FROM EMP WHERE SAL > (SELECT SAL FROM EMP WHERE EMPNO = 7566); 80 | 81 | ``` 82 | 83 | ## Q2)List the name of the employees whose job is equal to the job of employee with empno 7369 and salary is greater than that of employee with empno 7876. 84 | ```sql 85 | SELECT ENAME FROM EMP WHERE JOB = (SELECT JOB FROM EMP WHERE EMPNO = 7369) AND SAL > (SELECT SAL FROM EMP WHERE EMPNO = 7876); 86 | ``` 87 | 88 | ## Q3) List the ename,job,sal of the employee who get minimum salary in the company. 89 | ```sql 90 | SELECT ENAME, JOB, SAL FROM EMP WHERE SAL = (SELECT MIN(SAL) FROM EMP); 91 | ``` 92 | 93 | ## Q4) List deptno & min(salary) departmentwise, only if min(sal) is greater than the min(sal) of deptno 20. 94 | ```sql 95 | SELECT DEPTNO, MIN(SAL) FROM EMP GROUP BY DEPTNO HAVING MIN(SAL) > (SELECT MIN(SAL) FROM EMP WHERE DEPTNO = 20); 96 | ``` 97 | 98 | ## Q5) List empno, ename, job of the employees whose job is not a ‘CLERK’ and whose salary is less than at least one of the salaries of the employees whose job is ‘CLERK’. 99 | ```sql 100 | SELECT EMPNO, ENAME, JOB FROM EMP WHERE JOB <> 'CLERK' AND SAL < ANY(SELECT SAL FROM EMP WHERE JOB = 'CLERK'); 101 | ``` 102 | 103 | ## Q6) List empno, ename, job of the employees whose salary is greater than the average salary of each department. 104 | ```sql 105 | SELECT EMPNO, ENAME, JOB FROM EMP WHERE SAL > (SELECT AVG(SAL) FROM EMP WHERE DEPTNO = EMP.DEPTNO); 106 | ``` 107 | 108 | ## Q7) List ename, job, sal of the employees whose salary is equal to any one of the salary of the employee ‘SCOTT’ and ‘WARD’. 109 | ```sql 110 | SELECT ENAME, JOB, SAL FROM EMP WHERE SAL IN (SELECT SAL FROM EMP WHERE ENAME IN ('SCOTT', 'WARD')); 111 | ``` 112 | 113 | ## Q8) List ename, job, sal of the employees whose salary and job is equal to the employee ‘FORD’. 114 | ```sql 115 | SELECT ENAME, JOB, SAL FROM EMP WHERE SAL = (SELECT SAL FROM EMP WHERE ENAME = 'FORD') AND JOB = (SELECT JOB FROM EMP WHERE ENAME = 'FORD'); 116 | ``` 117 | 118 | ## Q9) List ename, job, deptno, sal of the employees whose job is same as ‘JONES’ and salary is greater than the employee ‘FORD’. 119 | ```sql 120 | SELECT ENAME, JOB, DEPTNO, SAL FROM EMP WHERE JOB = (SELECT JOB FROM EMP WHERE ENAME = 'JONES') AND SAL > (SELECT SAL FROM EMP WHERE ENAME = 'FORD'); 121 | ``` 122 | 123 | ## Q10) List ename, job of the employees who work in deptno 10 and his/her job is any one of the job in the department ‘SALES’. 124 | ```sql 125 | SELECT ENAME, JOB FROM EMP WHERE DEPTNO = 10 AND JOB IN (SELECT JOB FROM EMP WHERE DEPTNO = 30); 126 | ``` -------------------------------------------------------------------------------- /EXPT9.md: -------------------------------------------------------------------------------- 1 | ``` 2 | Give this Repository a ⭐️⭐️ Star ⭐️⭐️ for updates. 3 | COPY PASTE ALL THE QUERIES ONE BY ONE IN SQLPLUS TO EXECUTE IT WITHOUT ANY ERROR 4 | ``` 5 | # VIEWS 6 | ## Q1) Create a view empv10 that contains empno, ename, job of the employees who work in dept 10. Also describe the structure of the view. 7 | ```sql 8 | CREATE VIEW empv10 AS SELECT empno, ename, job FROM EMP WHERE deptno = 10; 9 | ``` 10 | 11 | ## Q2) Create a view with column aliases empv30 that contains empno, ename, sal of the employees who work in dept 30. Also display the contents of the view. 12 | ```sql 13 | CREATE VIEW empv30 AS SELECT empno AS "Employee Number", ename AS "Employee Name", sal AS "Salary" FROM EMP WHERE deptno = 30; 14 | ``` 15 | 16 | ## Q3) Update the view empv10 by increasing 10% salary of the employees who work as ‘CLERK’. Also confirm the modifications in emp table 17 | ```sql 18 | UPDATE empv10 SET sal = sal * 1.1 WHERE job = 'CLERK'; 19 | ``` 20 | 21 | ## Q4) Modify the view empv10 which contains the data empno, ename, job, sal. Add an alias for each column name. 22 | ```sql 23 | CREATE OR REPLACE VIEW empv10 AS SELECT empno AS "Employee Number", ename AS "Employee Name", job AS "Job Title", sal AS "Salary" FROM EMP WHERE deptno = 10; 24 | ``` 25 | 26 | ## Q5) Using emp table, create a view pay which contains ename, monthly_sal, annual_sal, deptno. 27 | ```sql 28 | CREATE VIEW pay AS SELECT ename, sal/12 AS monthly_sal, sal*12 AS annual_sal, deptno FROM EMP; 29 | 30 | ``` 31 | 32 | ## Q6) Create a view dept_stat which contains department no., department name, minimum salary, maximum salary, total salary. 33 | ```sql 34 | CREATE VIEW dept_stat AS SELECT d.deptno, d.dname, MIN(e.sal) AS min_sal, MAX(e.sal) AS max_sal, SUM(e.sal) AS total_sal FROM EMP e JOIN DEPT d ON e.deptno = d.deptno GROUP BY d.deptno, d.dname; 35 | ``` 36 | 37 | ## Q7) Execute the following query and then try to delete the row with dept no 20. Now write in words that you understand 38 | ```sql 39 | /* Note this query is the Question */ 40 | create or replace view empv20 41 | as select * from emp where deptno = 20 42 | with check option constraint empv20_ck; 43 | ``` 44 | ```sql 45 | /* Answer */ 46 | The given query creates or replaces a view named empv20 that selects all the columns from the EMP table where the deptno column equals 20. The view also has a check option constraint named empv20_ck. 47 | 48 | The check option constraint ensures that any data modification to the view only affects rows that meet the view's filter criteria. In this case, the constraint makes sure that any new or modified rows inserted into the empv20 view also have a deptno value of 20. This constraint helps maintain data consistency and integrity by preventing invalid data from being added to the view. 49 | 50 | Regarding the task of deleting the row with dept no 20, it is not possible to do so directly since the view empv20 only selects rows with deptno of 20. If you want to delete the row with deptno 20, you will need to delete it from the EMP table directly. However, if you try to delete a row from the EMP table that has a deptno of 20, the check option constraint named empv20_ck will prevent the deletion since it violates the constraint's rule that the deptno must be 20. 51 | ``` 52 | 53 | ## Q8) Create a view empv10 with all the details of employees who work in dept no. 10. 54 | ```sql 55 | CREATE VIEW empv10 AS SELECT * FROM emp WHERE deptno = 10 WITH READ ONLY; 56 | ``` 57 | ## Q9) Statement1 : Update the view will update data in original table. 58 | 59 | ## Statement 2: update in main table will affect the created view or not? 60 | 61 | ## State whether the above statements is True or False with explanation. 62 | ``` 63 | Statement 1 is True. Updating a view can update the underlying data in the original table. This happens when the view is based on a single table and the update statement only modifies columns in that table. In such cases, the update operation is passed through the view to the underlying table, and the data in the table is updated accordingly. 64 | 65 | Statement 2 is also True. If an update is made in the main table, it will affect the data in any views based on that table. Views are simply virtual tables that provide a different way of looking at the data in the original table. Therefore, any updates made to the original table will also be reflected in the views that are based on that table. 66 | 67 | It is important to note, however, that some views are based on more than one table, or use complex queries or aggregation functions, and in these cases, updates to the view may not be possible, or may not be passed through to the underlying tables. Additionally, views can have various constraints such as read-only or with-check options, which can further limit the ability to update the data in the underlying tables. 68 | ``` 69 | 70 | ## Q10) Delete the view empv20. 71 | ```sql 72 | DROP VIEW empv20; 73 | ``` -------------------------------------------------------------------------------- /Expt10.md: -------------------------------------------------------------------------------- 1 | ``` 2 | Give this Repository a ⭐️⭐️ Star ⭐️⭐️ for updates. 3 | COPY PASTE ALL THE QUERIES ONE BY ONE IN SQLPLUS TO EXECUTE IT WITHOUT ANY ERROR 4 | ``` 5 | 6 | # PL/SQL Conditional and Iterative Statements 7 | # ** Execute this command in sqlplus 1st ** 8 | ## TO display output in the screen – give the below command initially 9 | ```sql 10 | set serveroutput on; 11 | ``` 12 | ## 1. Get FARENHEIT and convert into CELSIUS 13 | 14 | ``` sql 15 | DECLARE 16 | celcius NUMBER; 17 | fahrenheit NUMBER; 18 | BEGIN 19 | celcius := &input_celcius; 20 | fahrenheit := 9/5 * celcius + 32; 21 | DBMS_OUTPUT.PUT_LINE (celcius ||' Celcius = '||fahrenheit|| ' Fahrenheit'); 22 | END; 23 | ``` 24 | 25 | ## 2. Write a pl/sql program to find SUM OF EVEN INTEGERS 26 | 27 | ``` sql 28 | DECLARE 29 | num NUMBER(3) := 2; 30 | sum1 NUMBER(4) := 0; 31 | BEGIN 32 | WHILE num <= 5 LOOP 33 | dbms_output.Put_line(num); 34 | sum1 := sum1 + num; 35 | num := num + 2; 36 | END LOOP; 37 | dbms_output.Put_line('Sum of even numbers is ' || sum1); 38 | END; 39 | ``` 40 | 41 | ## 3. Write a pl/sql program to find GREATEST OF THREE NUMBERS USING IF ELSEIF 42 | 43 | ``` sql 44 | DECLARE 45 | a NUMBER := 46; 46 | b NUMBER := 67; 47 | c NUMBER := 21; 48 | BEGIN 49 | IF a > b 50 | AND a > c THEN 51 | dbms_output.Put_line('Greatest number is ' 52 | ||a); 53 | ELSIF b > a 54 | AND b > c THEN 55 | dbms_output.Put_line('Greatest number is ' 56 | ||b); 57 | ELSE 58 | dbms_output.Put_line('Greatest number is ' 59 | ||c); 60 | END IF; 61 | END; 62 | ``` 63 | 64 | ## 4. Write a pl/sql program to find a number is ODD OR EVEN 65 | 66 | ``` sql 67 | DECLARE 68 | n1 NUMBER := &num1; 69 | BEGIN 70 | -- test if the number provided by the user is even 71 | IF MOD(n1,2) = 0 THEN 72 | DBMS_OUTPUT.PUT_LINE ('The number. '||n1|| 73 | ' is even number'); 74 | ELSE 75 | DBMS_OUTPUT.PUT_LINE ('The number '||n1||' is odd number.'); 76 | END IF; 77 | DBMS_OUTPUT.PUT_LINE ('Done Successfully'); 78 | END; 79 | ``` 80 | 81 | ## 5. Write a pl/sql program to find a FACTORIAL OF A NUMBER 82 | 83 | ``` sql 84 | DECLARE 85 | num NUMBER := # 86 | factorial NUMBER := 1; 87 | BEGIN 88 | FOR i IN 1..num LOOP 89 | factorial := factorial * i; 90 | END LOOP; 91 | 92 | DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is ' || factorial); 93 | END; 94 | ``` 95 | 96 | 97 | -------------------------------------------------------------------------------- /Expt12.md: -------------------------------------------------------------------------------- 1 | ``` 2 | Give this Repository a ⭐️⭐️ Star ⭐️⭐️ for updates. 3 | COPY PASTE ALL THE QUERIES ONE BY ONE IN SQLPLUS TO EXECUTE IT WITHOUT ANY ERROR 4 | ``` 5 | 6 | # PL/SQL Functions 7 | # ** Execute this command in sqlplus 1st ** 8 | ## TO display output in the screen – give the below command initially 9 | ```sql 10 | set serveroutput on; 11 | ``` 12 | ## 1) Write a pl/sql function recursive code for finding factorial of a number. 13 | ```sql 14 | CREATE OR REPLACE FUNCTION factorial(n IN NUMBER) RETURN NUMBER 15 | IS 16 | BEGIN 17 | -- base case 18 | IF n = 0 OR n = 1 THEN 19 | RETURN 1; 20 | ELSE 21 | -- recursive case 22 | RETURN n * factorial(n - 1); 23 | END IF; 24 | END; 25 | / 26 | ``` 27 | ```sql 28 | DECLARE 29 | n NUMBER := 5; 30 | fact NUMBER; 31 | BEGIN 32 | fact := factorial(n); 33 | DBMS_OUTPUT.PUT_LINE('Factorial of ' || n || ' is ' || fact); 34 | END; 35 | / 36 | ``` 37 | 38 | ## 2) Write a pl/sql function for finding a number is a prime number. 39 | ```sql 40 | CREATE OR REPLACE FUNCTION is_prime(n IN NUMBER) RETURN BOOLEAN 41 | IS 42 | divisor NUMBER := 2; 43 | BEGIN 44 | IF n <= 1 THEN 45 | RETURN FALSE; 46 | END IF; 47 | 48 | WHILE divisor <= SQRT(n) LOOP 49 | IF MOD(n, divisor) = 0 THEN 50 | RETURN FALSE; 51 | END IF; 52 | divisor := divisor + 1; 53 | END LOOP; 54 | 55 | RETURN TRUE; 56 | END; 57 | / 58 | ``` 59 | ```sql 60 | DECLARE 61 | n NUMBER := 17; 62 | BEGIN 63 | IF is_prime(n) THEN 64 | DBMS_OUTPUT.PUT_LINE(n || ' is a prime number.'); 65 | ELSE 66 | DBMS_OUTPUT.PUT_LINE(n || ' is not a prime number.'); 67 | END IF; 68 | END; 69 | / 70 | ``` 71 | ## Student Table Creation for 3rd Question 72 | ``` sql 73 | CREATE TABLE Student ( 74 | Sno NUMBER, 75 | sname VARCHAR2(50), 76 | dept VARCHAR2(50), 77 | cgpa NUMBER 78 | ); 79 | ``` 80 | ## Values 81 | ```sql 82 | INSERT INTO Student (Sno, sname, dept, cgpa) 83 | VALUES (1, 'John', 'CSE', 8.5); 84 | 85 | INSERT INTO Student (Sno, sname, dept, cgpa) 86 | VALUES (2, 'Jane', 'CSE', 9.0); 87 | 88 | INSERT INTO Student (Sno, sname, dept, cgpa) 89 | VALUES (3, 'Mike', 'CSE', 7.5); 90 | ``` 91 | ## 3) Write a pl/sql function to retrieve the count of students from ‘CSE’ department from the table Student (Sno, sname, dept, cgpa). 92 | ```sql 93 | CREATE OR REPLACE FUNCTION get_cse_student_count RETURN NUMBER 94 | IS 95 | cse_count NUMBER; 96 | BEGIN 97 | SELECT COUNT(*) INTO cse_count FROM Student WHERE dept = 'CSE'; 98 | RETURN cse_count; 99 | END; 100 | / 101 | ``` 102 | ```sql 103 | DECLARE 104 | cse_count NUMBER; 105 | BEGIN 106 | cse_count := get_cse_student_count; 107 | DBMS_OUTPUT.PUT_LINE('Number of students in CSE department: ' || cse_count); 108 | END; 109 | / 110 | ``` 111 | 112 | ## 4) Write a pl/sql function to retrieve the maximum CGPA of the student from the table Student (Sno, sname, dept, cgpa). 113 | ```sql 114 | CREATE OR REPLACE FUNCTION get_max_cgpa RETURN NUMBER 115 | IS 116 | max_cgpa NUMBER; 117 | BEGIN 118 | SELECT MAX(cgpa) INTO max_cgpa FROM Student; 119 | RETURN max_cgpa; 120 | END; 121 | / 122 | ``` 123 | ```sql 124 | DECLARE 125 | max_cgpa NUMBER; 126 | BEGIN 127 | max_cgpa := get_max_cgpa; 128 | DBMS_OUTPUT.PUT_LINE('Maximum CGPA: ' || max_cgpa); 129 | END; 130 | / 131 | ``` 132 | 133 | ## 5) Write a simple PL/SQL Function that computes and returns the average of two numbers. 134 | ```sql 135 | CREATE OR REPLACE FUNCTION average_of_two_numbers ( 136 | num1 NUMBER, 137 | num2 NUMBER 138 | ) RETURN NUMBER IS 139 | avg_num NUMBER; 140 | BEGIN 141 | avg_num := (num1 + num2) / 2; 142 | RETURN avg_num; 143 | END; 144 | / 145 | ``` 146 | ```sql 147 | SELECT average_of_two_numbers(10, 20) FROM dual; 148 | ``` 149 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Dharsan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DBMS LAB 2 | ``` 3 | The SQL Queries for the Database Management System LAB Course are contained in this repository. Every week, this repo will be updated for the exercise. If you liked it, be sure to tell your friends about this repository and Give this Repository a ⭐️⭐️ Star ⭐️⭐️ for updates. 4 | 5 | If it has any error feel free to make a PR ❕❕❕ 6 | 7 | 🌟 Star 🌟 && Share 🤟🏻 8 | 9 | 🤍 Happy Coding 🤍 10 | ``` 11 | 15 | --------------------------------------------------------------------------------