O slideshow foi denunciado.

SQL commands

3

Compartilhar

Carregando em…3
×
1 de 10
1 de 10

SQL commands

3

Compartilhar

Baixar para ler offline

Descrição

SQL some basic commands

Transcrição

  1. 1. SQL Database and Management System By Manish
  2. 2. SQL BASIS COMMANDS
  3. 3. CREATE COMMAND: 1. EMPLOYEE TABLE: CREATE TABLE EMPLOYEE ( Fname varchar(15) not null, Minitchar, Lname varchar(15) not null, ssn char(9) not null primarykey, Bdate Date, Addressvarchar(30), Sex char, Salarynumber, Super_ssnchar(9) notnull , Dnoint ); 2. DEPARTMENT TABLE: CREATE table department ( Dname varchar(15) not null, DnumberINTegernotnull PRIMARYKEY, mgr_ssn char(9) not null, mrg_start_date date ); 3. DEPT_LOCATION TABLE: CREATE TABLE DEPT_LOCATION ( DnumberintegernotnulL, Dlocationvarchar(15) not null, PRIMARYKEY(Dnumber,Dlocation) );
  4. 4. 4. WORKS_ON TABLE: CREATE TABLE WORKS_ON ( Essnchar(9) not null, PnoINt, hoursnumber, PRIMARYKEY(ESSN,PNO) ); 5. PROJECT TABLE: CREATE TABLE PROJECT ( Pname varchar(10) not null, PnumberINTnotnull PRIMARY KEY, Plocationvarchar(15), DmunINT ); 6. DEPENDENT TABLE: CREATE TABLE DEPENDENT ( Essn char(9) notnull, Dependent_namevarchar(15) notnull PRIMARY KEY, Sex char , Bdate time, Relationshipvarchar(10) );
  5. 5. INSERT COMMAND: INSERTING TUPLES INTO EMPLOYEE TABLE: 1. INSERT INTO EMPLOYEE VALUES (‘John’,’B’,’Smith’,’123456789’,’1965-01-09’,’731 Fondren, Houston, TX’,’M’,30000,333445555,5) 2. INSERT INTO EMPLOYEE VALUES (‘Franklin’,’T’,’Wong’,333445555’,’1955-13-08’,’638 Voss,Houston, TX’,’M’,40000,888665555,5) 3. INSERT INTO EMPLOYEE VALUES (‘Alicia’,’J’,’Zelaya’,’999887777’,’1968-01-19’,’3321 Castle,Spring, TX’,’F’,25000,987654321,4) 4. INSERT INTO EMPLOYEE VALUES (‘Jennifer’,’S’,’Wallace’,’987654321’,’1941-06-20’,’291 Berry,Bellaire, TX’,’F’,43000,888665555,4) 5. INSERT INTO EMPLOYEE VALUES (‘Ramesh’,’K’,’Narayan’,’666884444’,’1962-09-15’,’975 FireOak, Humble, TX’,’M’,38000,333445555,5) 6. INSERT INTO EMPLOYEE VALUES (‘Joyce’,’A’,’English’,’453453453’,’1972-07-31’,’5631 Rice ,Houston,TX’,’F’,25000,333445555,5) 7. INSERT INTO EMPLOYEE VALUES (‘Ahmed’,’V’,’Jabbar’,’987987987’,’1969-03-29’,’980 Dallas,Houston, TX’,’M’,25000,987654321,4) 8. INSERT INTO EMPLOYEE VALUES (‘James’,’E’,’Borg’,’888665555’,’1937-11-10’,’450 Stone, Houston, TX’,’M’,55000,NULL,1) EMPLOYEE Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno Ahmad V Jabbar 987987987 29-Mar-69 980,Dallas Houstontx M 25000 987654321 4 Alicia J Zelaya 999887777 19-Jan-68 3321 castle,springtx F 25000 987654321 4 FranklinT Wong 333445555 08-Dec-55 638 voss,Houstontx M 40000 888665555 5 James E Borg 888665555 10-Nov-37 450 Stone ,Houstontx M 55000 1 JenniferS Wallace 987654321 20-Jun-41 291 berry,Bellaire,tx F 43000 888665555 4 John B Smith 123456789 09-Jan-65 731 Fondren,Houston,TX M 30000 333445555 5 Joyce A English 453453453 31-Jul-72 5631 Rice,Houstontx F 25000 333445555 5 RameshK Narayan 666884444 15-Sep-62 975,Fire Oak ,Humble,tx M 38000 333445555 5 INSERTING TUPLES INTO DEPARTMENT TABLE: 1. INSERT INTO DEPARTMENT VALUES (‘Research’,5,333445555,’1988-05-22’) 2. INSERT INTO DEPARTMENT VALUES (‘Administration’,4,987654321,’1995-01-01’) 3. INSERT INTO DEPARTMENT VALUES (‘Headquarters’1,888665555,’1981-06-19’) DEPARTMENT Dname Dnumber Mgr_ssn Mgr_start_date RESEARCH 1 888665555 19-Jun-81 Administration 4 987654321 01-Jan-95 Research 5 333445555 22-May-88
  6. 6. INSERTING TUPLES INTO DEPT_LOCATIONS TABLE: 1. INSERT INTO DEPT_LOCATIONS VALUES (1,’Houston’) 2. INSERT INTO DEPT_LOCATIONS VALUES (4,’Stafford’) 3. INSERT INTO DEPT_LOCATIONS VALUES (5,’Bellaire’) 4. INSERT INTO DEPT_LOCATIONS VALUES (5,’Sugarland’) 5. INSERT INTO DEPT_LOCATIONS VALUES (5,’Houston’) INSERTING TUPLES INTO WORKS_ON TABLE: 1. INSERT INTO WORKS_ON VALUES(123456789,1,32.5) 2. INSERT INTO WORKS_ON VALUES(123456789,2,7.5) 3. INSERT INTO WORKS_ON VALUES(666884444,3,40.0) 4. INSERT INTO WORKS_ON VALUES(453453453,1,20.0) 5. INSERT INTO WORKS_ON VALUES(453453453,2,20.0) 6. INSERT INTO WORKS_ON VALUES(333445555,2,10.0) 7. INSERT INTO WORKS_ON VALUES(333445555,3,10.0) 8. INSERT INTO WORKS_ON VALUES(333445555,10,10.0) 9. INSERT INTO WORKS_ON VALUES(333445555,20,10.0) 10. INSERT INTO WORKS_ON VALUES(999887777,30,30.0) 11. INSERT INTO WORKS_ON VALUES(999887777,10,10.0) 12. INSERT INTO WORKS_ON VALUES(987987987,10,10.0) 13. INSERT INTO WORKS_ON VALUES(987987987,30,5.0) 14. INSERT INTO WORKS_ON VALUES(987654321,30,20.0) 15. INSERT INTO WORKS_ON VALUES(987654321,20,15.0) 16. INSERT INTO WORKS_ON VALUES(888665555,20,NULL) INSERTING TUPLES INTO PROJECT TABLE: 1. INSERT INTO PROJECT VALUES(‘ProductX’,1,’Bellaire’,5) 2. INSERT INTO PROJECT VALUES(‘ProductY’,2,’Sugarland’,5) 3. INSERT INTO PROJECT VALUES(‘ProductZ’,3,’Houston’,5) 4. INSERT INTO PROJECT VALUES(‘Computerization’,10,’Stafford’,4) 5. INSERT INTO PROJECT VALUES(‘Reorganization’,20,’Houston’,1) 6. INSERT INTO PROJECT VALUES(‘Newbenefits’30,’Stafford’,4) PROJECT Pname Pnumber Plocation Dnum ProductX 1 Bellaire 5 ProductY 2 Sugarland 5 ProductZ 3 Houston 5 Computerization 10 Stafford 4 Reorganization 20 Houston 1 Newbenefits 30 Stafford 4 DEPT_LOCATION Dnumber Dlocation 1 Houston 4 Stafford 5 Bellaire 5 Houston 5 Sugarland WORKS_ON Essn Pno Hours 123456789 1 32 123456789 2 7.5 666884444 3 40 435453453 1 20 435453453 2 20 333445555 2 10 333445555 3 10 333445555 10 10 333445555 10 10 333445555 20 10 999887777 30 30 999887777 10 10 987987987 10 35 987987987 30 5 987654321 30 20 987654321 20 15 888665555 20
  7. 7. INSERTING TUPLES INTO DEPENDENT TABLE: 1. INSERT INTO DEPENDENT VALUES(‘333445555’,’Alice’,’F’,’1986-04-05’,’Daughter’) 2. INSERT INTO DEPENDENT VALUES(‘333445555’,’Theodore’,’M’,’1983-10-25’,’Son’) 3. INSERT INTO DEPENDENT VALUES(‘333445555’,’Joy’,’F’,’1958-05-03’,’Spouse’) 4. INSERT INTO DEPENDENT VALUES(‘987654321’,’Abner’,’1942-02-28’,’Spouse’) 5. INSERT INTO DEPENDENT VALUES(‘123456789’,’Michael’,’1988-01-04’,’Son’) 6. INSERT INTO DEPENDENT VALUES(‘123456789’,’Alice’,’F’,’1988-12-30’,’Daughter’) 7. INSERT INTO DEPENDENT VALUES(‘123456789’,’Elizabeth’,’F’,’1967-05-05’,’Spouse’) DEPENDENT Essn Dependent_name Sex Bdate Relationship 333445555 Alice F 05-Apr-86 Daughter 333445555 Theodore M 25-Oct-83 Son 333445555 Joy F 03-May-58 Spouse 987654321 Abner M 28-Feb-42 Spouse 123456789 Michael M 04-Jan-88 Son 123456789 Alice F 30-Dec-88 Daughter 123456789 Elizabeth F 05-May-67 Spouse DELETE COMMAND: 1. DELETE FROM PROJECT WHERE Pname=’ProductX’ 2. DELETE FROM PROJECT WHERE Pnumber=3 UPDATE COMMAND: 1. UPDATE DEPARTMENT SET Dname=’Research’ WHERE Dnum=1 PROJECT Pname Pnumber Plocation Dnum #Deleted #Deleted ProductY 2 Sugarland 5 #Deleted #Deleted Computerization 10 Stafford 4 Reorganisation 20 Houston 1 Newbenefits 30 Stafford 4 DEPARTMENT Dname Dnumber Mgr_ssn Mgr_start_date Research 1 888665555 19-Jun-81 Administration 4 987654321 01-Jan-95 Research 5 333445555 22-May-88
  8. 8. Q1. Retrieve the birthdate andaddressof the employeewhose name is‘JohnBSmith’. SELECT Bdate ,Address FROM EMPLOYEE WHERE Fname='john'ANDLname='Smith' Q2. Retrieve the name andaddressof the all employee whoworkforthe ‘Research’Department. SELECT Fname ,Minit,Address FROM EMPLOYEE,DEPARTMENT WHERE Dname='RESEARCH'AND Dnumber=Dno Q3.For everyprojectlocatedinthe ‘Stafford’,listthe projectnumber,the controllingdepartment numberandthe departmentmanagerlastname ,addressandbirthdate. SELECT Pnumber,Dnum,Lname,Address,Bdate FROM PROJECT,DEPARTMENT,EMPLOYEE WHERE project.dnum=department.dnumberand department.mgr_ssn=employee.ssnand project.plocation='Stafford' Q4.For each employeeretrieve the employeefirstandlastname andthe firstand last name of hisor her immediate supervisior. SELECT E1.Fname,E1.Lname ,E2.Fname ASSupervisior_Fname,E2.LnameasSupervisior_Lname FROM EMPLOYEE ASE1,EMPLOYEE AS E2 WHERE E1.super_ssn=E2.ssn Q1 Bdate Address 09-Jan-65 731 Fondren Houston,TX Q2 Fname Minit Lname Address John B Smith 731 FondrenHouston, TX Franklin T Wong 638 Voss,Houston,TX Joyce A English 5631 Rice,Houston,TX Ramesh K Narayan 975,Fire Oak ,Humble,TX Q3 Pnumber Dnum Lname Address Bdate 10 4 Wallace 291 Berry,Bellaire, TX 20-Jun-41 30 4 Wallace 291 Berry,Bellaire,TX 20-Jun-41 Q4 Fname Lname Supervisior_Fname Supervisior_Lname John Smith Franklin Wong Joyce English Franklin Wong RameshNarayan Franklin Wong FranklinWong James Borg JenniferWallace James Borg Ahmad Jabbar Jennifer Wallace Alicia Zelaya Jennifer Wallace
  9. 9. Q5.Make a listof the all projectnumberforprojectsthat involve anemployeewhoselastname is ‘Smith’eitherasa workeroras Manager of the departmentthatcontrolsthe projects. (SELECT DISTINCT Pnumber FROM PROJECT,DEPARTMENT,EMPLOYEE WHERE Dnum=Dnumberandmgr_ssn=ssnandLname='Smith') UNION (SELECT DISTINCTPnumber FROM PROJECT,WORKS_ON ,EMPLOYEE WHERE Pnumber=Pnoand Essn=ssnand Lname='Smith') Q6.Retrieve all the employeewhose AddressisinHoustonTexas. SELECT Fname,Minit,Lname,Address FROM EMPLOYEE WHERE AddressLike '*Houston*' Q7.Findall employee whowere bornduring1955’s. SELECT Fname,Lname,Bdate FROM EMPLOYEE WHERE year(bdate)='1955' Q8.Showthe increasedsalariesif everyemployeeworkingon‘ProductX’projectisgivena10 percent raise. SELECT Fname,Minit,Lname,1.1*SalaryasIncreased_Salary FROM EMPLOYEE,WORKS_ON,PROJECT WHERE ssn=Essnand Pno=PnumberandPname ='ProductX' Q9.Retrieve all employee indepartment5whose salaryisb/w $30000 and $40000. SELECT Fname ,Minit,Lname,Salary FROM EMPLOYEE WHERE salarybetween30000 and 40000 Q5 Pnumber 1 2 Q6 Fname Minit Lname Address John B Smith 731 FondrenHouston,TX Franklin T Wong 638 Voss,Houston,TX Joyce A English 5631 Rice, Houston,TX James E Borg 450 Stone ,Houston,TX Ahmad V Jabbar 980,Dallas Houston,TX Q7 Fname Lname Bdate FranklinWong 08-Dec-55 Q8 Fname Minit Lname Increased_Salary John B Smith 33000 Q9 Fname Minit Lname Salary John B Smith 30000 Franklin T Wong 40000 Ramesh K Narayan 38000
  10. 10. Q10.Retrieve the name of the employee andhissalarywhoisearningMaximum. SELECT MAX(Salary) as[Max_sal] FROM EMPLOYEE Q11.Retrieve the salaryof the 2nd Highestearningemployeeandit’sname. SELECT Fname,Minit,Lname,salary FROM EMPLOYEE WHERE Salary=(SELECTMAX(salary) FROMemployee WHEREsalary<(SELECT MAX(salary) FROM employee )) Q12.Retrieve alistof the employeeandthe Projectstheyare workingonorderedbydepartmentand withindepartmentorderedbythe alphabeticallybylastname thenfirstname. SELECT Dname ,Lname ,Fname ,Pname FROM EMPLOYEE,DEPARTMENT,PROJECT,WORKS_ON WHERE Dnumber=dnoandssn=essnandpno=Pnumber orderby dname ,Lname,Fname Q10 Max_sal 55000 Q11 Fname Minit Lname salary JenniferS Wallace 43000 Q12 Dname Lname Fname Pname Administration Jabbar Ahmad Newbenefits Administration Jabbar Ahmad Computerization Administration Wallace Jennifer Newbenefits Administration Wallace Jennifer Reorganisation Administration Zelaya Alicia Newbenefits Administration Zelaya Alicia Computerization Headquarters Borg James Reorganisation Research Narayan Ramesh ProductZ Research Smith John ProductY Research Smith John ProductX Research Wong Franklin Reorganisation Research Wong Franklin Computerization Research Wong Franklin Computerization Research Wong Franklin ProductZ Research Wong Franklin ProductY

Descrição

SQL some basic commands

Transcrição

  1. 1. SQL Database and Management System By Manish
  2. 2. SQL BASIS COMMANDS
  3. 3. CREATE COMMAND: 1. EMPLOYEE TABLE: CREATE TABLE EMPLOYEE ( Fname varchar(15) not null, Minitchar, Lname varchar(15) not null, ssn char(9) not null primarykey, Bdate Date, Addressvarchar(30), Sex char, Salarynumber, Super_ssnchar(9) notnull , Dnoint ); 2. DEPARTMENT TABLE: CREATE table department ( Dname varchar(15) not null, DnumberINTegernotnull PRIMARYKEY, mgr_ssn char(9) not null, mrg_start_date date ); 3. DEPT_LOCATION TABLE: CREATE TABLE DEPT_LOCATION ( DnumberintegernotnulL, Dlocationvarchar(15) not null, PRIMARYKEY(Dnumber,Dlocation) );
  4. 4. 4. WORKS_ON TABLE: CREATE TABLE WORKS_ON ( Essnchar(9) not null, PnoINt, hoursnumber, PRIMARYKEY(ESSN,PNO) ); 5. PROJECT TABLE: CREATE TABLE PROJECT ( Pname varchar(10) not null, PnumberINTnotnull PRIMARY KEY, Plocationvarchar(15), DmunINT ); 6. DEPENDENT TABLE: CREATE TABLE DEPENDENT ( Essn char(9) notnull, Dependent_namevarchar(15) notnull PRIMARY KEY, Sex char , Bdate time, Relationshipvarchar(10) );
  5. 5. INSERT COMMAND: INSERTING TUPLES INTO EMPLOYEE TABLE: 1. INSERT INTO EMPLOYEE VALUES (‘John’,’B’,’Smith’,’123456789’,’1965-01-09’,’731 Fondren, Houston, TX’,’M’,30000,333445555,5) 2. INSERT INTO EMPLOYEE VALUES (‘Franklin’,’T’,’Wong’,333445555’,’1955-13-08’,’638 Voss,Houston, TX’,’M’,40000,888665555,5) 3. INSERT INTO EMPLOYEE VALUES (‘Alicia’,’J’,’Zelaya’,’999887777’,’1968-01-19’,’3321 Castle,Spring, TX’,’F’,25000,987654321,4) 4. INSERT INTO EMPLOYEE VALUES (‘Jennifer’,’S’,’Wallace’,’987654321’,’1941-06-20’,’291 Berry,Bellaire, TX’,’F’,43000,888665555,4) 5. INSERT INTO EMPLOYEE VALUES (‘Ramesh’,’K’,’Narayan’,’666884444’,’1962-09-15’,’975 FireOak, Humble, TX’,’M’,38000,333445555,5) 6. INSERT INTO EMPLOYEE VALUES (‘Joyce’,’A’,’English’,’453453453’,’1972-07-31’,’5631 Rice ,Houston,TX’,’F’,25000,333445555,5) 7. INSERT INTO EMPLOYEE VALUES (‘Ahmed’,’V’,’Jabbar’,’987987987’,’1969-03-29’,’980 Dallas,Houston, TX’,’M’,25000,987654321,4) 8. INSERT INTO EMPLOYEE VALUES (‘James’,’E’,’Borg’,’888665555’,’1937-11-10’,’450 Stone, Houston, TX’,’M’,55000,NULL,1) EMPLOYEE Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno Ahmad V Jabbar 987987987 29-Mar-69 980,Dallas Houstontx M 25000 987654321 4 Alicia J Zelaya 999887777 19-Jan-68 3321 castle,springtx F 25000 987654321 4 FranklinT Wong 333445555 08-Dec-55 638 voss,Houstontx M 40000 888665555 5 James E Borg 888665555 10-Nov-37 450 Stone ,Houstontx M 55000 1 JenniferS Wallace 987654321 20-Jun-41 291 berry,Bellaire,tx F 43000 888665555 4 John B Smith 123456789 09-Jan-65 731 Fondren,Houston,TX M 30000 333445555 5 Joyce A English 453453453 31-Jul-72 5631 Rice,Houstontx F 25000 333445555 5 RameshK Narayan 666884444 15-Sep-62 975,Fire Oak ,Humble,tx M 38000 333445555 5 INSERTING TUPLES INTO DEPARTMENT TABLE: 1. INSERT INTO DEPARTMENT VALUES (‘Research’,5,333445555,’1988-05-22’) 2. INSERT INTO DEPARTMENT VALUES (‘Administration’,4,987654321,’1995-01-01’) 3. INSERT INTO DEPARTMENT VALUES (‘Headquarters’1,888665555,’1981-06-19’) DEPARTMENT Dname Dnumber Mgr_ssn Mgr_start_date RESEARCH 1 888665555 19-Jun-81 Administration 4 987654321 01-Jan-95 Research 5 333445555 22-May-88
  6. 6. INSERTING TUPLES INTO DEPT_LOCATIONS TABLE: 1. INSERT INTO DEPT_LOCATIONS VALUES (1,’Houston’) 2. INSERT INTO DEPT_LOCATIONS VALUES (4,’Stafford’) 3. INSERT INTO DEPT_LOCATIONS VALUES (5,’Bellaire’) 4. INSERT INTO DEPT_LOCATIONS VALUES (5,’Sugarland’) 5. INSERT INTO DEPT_LOCATIONS VALUES (5,’Houston’) INSERTING TUPLES INTO WORKS_ON TABLE: 1. INSERT INTO WORKS_ON VALUES(123456789,1,32.5) 2. INSERT INTO WORKS_ON VALUES(123456789,2,7.5) 3. INSERT INTO WORKS_ON VALUES(666884444,3,40.0) 4. INSERT INTO WORKS_ON VALUES(453453453,1,20.0) 5. INSERT INTO WORKS_ON VALUES(453453453,2,20.0) 6. INSERT INTO WORKS_ON VALUES(333445555,2,10.0) 7. INSERT INTO WORKS_ON VALUES(333445555,3,10.0) 8. INSERT INTO WORKS_ON VALUES(333445555,10,10.0) 9. INSERT INTO WORKS_ON VALUES(333445555,20,10.0) 10. INSERT INTO WORKS_ON VALUES(999887777,30,30.0) 11. INSERT INTO WORKS_ON VALUES(999887777,10,10.0) 12. INSERT INTO WORKS_ON VALUES(987987987,10,10.0) 13. INSERT INTO WORKS_ON VALUES(987987987,30,5.0) 14. INSERT INTO WORKS_ON VALUES(987654321,30,20.0) 15. INSERT INTO WORKS_ON VALUES(987654321,20,15.0) 16. INSERT INTO WORKS_ON VALUES(888665555,20,NULL) INSERTING TUPLES INTO PROJECT TABLE: 1. INSERT INTO PROJECT VALUES(‘ProductX’,1,’Bellaire’,5) 2. INSERT INTO PROJECT VALUES(‘ProductY’,2,’Sugarland’,5) 3. INSERT INTO PROJECT VALUES(‘ProductZ’,3,’Houston’,5) 4. INSERT INTO PROJECT VALUES(‘Computerization’,10,’Stafford’,4) 5. INSERT INTO PROJECT VALUES(‘Reorganization’,20,’Houston’,1) 6. INSERT INTO PROJECT VALUES(‘Newbenefits’30,’Stafford’,4) PROJECT Pname Pnumber Plocation Dnum ProductX 1 Bellaire 5 ProductY 2 Sugarland 5 ProductZ 3 Houston 5 Computerization 10 Stafford 4 Reorganization 20 Houston 1 Newbenefits 30 Stafford 4 DEPT_LOCATION Dnumber Dlocation 1 Houston 4 Stafford 5 Bellaire 5 Houston 5 Sugarland WORKS_ON Essn Pno Hours 123456789 1 32 123456789 2 7.5 666884444 3 40 435453453 1 20 435453453 2 20 333445555 2 10 333445555 3 10 333445555 10 10 333445555 10 10 333445555 20 10 999887777 30 30 999887777 10 10 987987987 10 35 987987987 30 5 987654321 30 20 987654321 20 15 888665555 20
  7. 7. INSERTING TUPLES INTO DEPENDENT TABLE: 1. INSERT INTO DEPENDENT VALUES(‘333445555’,’Alice’,’F’,’1986-04-05’,’Daughter’) 2. INSERT INTO DEPENDENT VALUES(‘333445555’,’Theodore’,’M’,’1983-10-25’,’Son’) 3. INSERT INTO DEPENDENT VALUES(‘333445555’,’Joy’,’F’,’1958-05-03’,’Spouse’) 4. INSERT INTO DEPENDENT VALUES(‘987654321’,’Abner’,’1942-02-28’,’Spouse’) 5. INSERT INTO DEPENDENT VALUES(‘123456789’,’Michael’,’1988-01-04’,’Son’) 6. INSERT INTO DEPENDENT VALUES(‘123456789’,’Alice’,’F’,’1988-12-30’,’Daughter’) 7. INSERT INTO DEPENDENT VALUES(‘123456789’,’Elizabeth’,’F’,’1967-05-05’,’Spouse’) DEPENDENT Essn Dependent_name Sex Bdate Relationship 333445555 Alice F 05-Apr-86 Daughter 333445555 Theodore M 25-Oct-83 Son 333445555 Joy F 03-May-58 Spouse 987654321 Abner M 28-Feb-42 Spouse 123456789 Michael M 04-Jan-88 Son 123456789 Alice F 30-Dec-88 Daughter 123456789 Elizabeth F 05-May-67 Spouse DELETE COMMAND: 1. DELETE FROM PROJECT WHERE Pname=’ProductX’ 2. DELETE FROM PROJECT WHERE Pnumber=3 UPDATE COMMAND: 1. UPDATE DEPARTMENT SET Dname=’Research’ WHERE Dnum=1 PROJECT Pname Pnumber Plocation Dnum #Deleted #Deleted ProductY 2 Sugarland 5 #Deleted #Deleted Computerization 10 Stafford 4 Reorganisation 20 Houston 1 Newbenefits 30 Stafford 4 DEPARTMENT Dname Dnumber Mgr_ssn Mgr_start_date Research 1 888665555 19-Jun-81 Administration 4 987654321 01-Jan-95 Research 5 333445555 22-May-88
  8. 8. Q1. Retrieve the birthdate andaddressof the employeewhose name is‘JohnBSmith’. SELECT Bdate ,Address FROM EMPLOYEE WHERE Fname='john'ANDLname='Smith' Q2. Retrieve the name andaddressof the all employee whoworkforthe ‘Research’Department. SELECT Fname ,Minit,Address FROM EMPLOYEE,DEPARTMENT WHERE Dname='RESEARCH'AND Dnumber=Dno Q3.For everyprojectlocatedinthe ‘Stafford’,listthe projectnumber,the controllingdepartment numberandthe departmentmanagerlastname ,addressandbirthdate. SELECT Pnumber,Dnum,Lname,Address,Bdate FROM PROJECT,DEPARTMENT,EMPLOYEE WHERE project.dnum=department.dnumberand department.mgr_ssn=employee.ssnand project.plocation='Stafford' Q4.For each employeeretrieve the employeefirstandlastname andthe firstand last name of hisor her immediate supervisior. SELECT E1.Fname,E1.Lname ,E2.Fname ASSupervisior_Fname,E2.LnameasSupervisior_Lname FROM EMPLOYEE ASE1,EMPLOYEE AS E2 WHERE E1.super_ssn=E2.ssn Q1 Bdate Address 09-Jan-65 731 Fondren Houston,TX Q2 Fname Minit Lname Address John B Smith 731 FondrenHouston, TX Franklin T Wong 638 Voss,Houston,TX Joyce A English 5631 Rice,Houston,TX Ramesh K Narayan 975,Fire Oak ,Humble,TX Q3 Pnumber Dnum Lname Address Bdate 10 4 Wallace 291 Berry,Bellaire, TX 20-Jun-41 30 4 Wallace 291 Berry,Bellaire,TX 20-Jun-41 Q4 Fname Lname Supervisior_Fname Supervisior_Lname John Smith Franklin Wong Joyce English Franklin Wong RameshNarayan Franklin Wong FranklinWong James Borg JenniferWallace James Borg Ahmad Jabbar Jennifer Wallace Alicia Zelaya Jennifer Wallace
  9. 9. Q5.Make a listof the all projectnumberforprojectsthat involve anemployeewhoselastname is ‘Smith’eitherasa workeroras Manager of the departmentthatcontrolsthe projects. (SELECT DISTINCT Pnumber FROM PROJECT,DEPARTMENT,EMPLOYEE WHERE Dnum=Dnumberandmgr_ssn=ssnandLname='Smith') UNION (SELECT DISTINCTPnumber FROM PROJECT,WORKS_ON ,EMPLOYEE WHERE Pnumber=Pnoand Essn=ssnand Lname='Smith') Q6.Retrieve all the employeewhose AddressisinHoustonTexas. SELECT Fname,Minit,Lname,Address FROM EMPLOYEE WHERE AddressLike '*Houston*' Q7.Findall employee whowere bornduring1955’s. SELECT Fname,Lname,Bdate FROM EMPLOYEE WHERE year(bdate)='1955' Q8.Showthe increasedsalariesif everyemployeeworkingon‘ProductX’projectisgivena10 percent raise. SELECT Fname,Minit,Lname,1.1*SalaryasIncreased_Salary FROM EMPLOYEE,WORKS_ON,PROJECT WHERE ssn=Essnand Pno=PnumberandPname ='ProductX' Q9.Retrieve all employee indepartment5whose salaryisb/w $30000 and $40000. SELECT Fname ,Minit,Lname,Salary FROM EMPLOYEE WHERE salarybetween30000 and 40000 Q5 Pnumber 1 2 Q6 Fname Minit Lname Address John B Smith 731 FondrenHouston,TX Franklin T Wong 638 Voss,Houston,TX Joyce A English 5631 Rice, Houston,TX James E Borg 450 Stone ,Houston,TX Ahmad V Jabbar 980,Dallas Houston,TX Q7 Fname Lname Bdate FranklinWong 08-Dec-55 Q8 Fname Minit Lname Increased_Salary John B Smith 33000 Q9 Fname Minit Lname Salary John B Smith 30000 Franklin T Wong 40000 Ramesh K Narayan 38000
  10. 10. Q10.Retrieve the name of the employee andhissalarywhoisearningMaximum. SELECT MAX(Salary) as[Max_sal] FROM EMPLOYEE Q11.Retrieve the salaryof the 2nd Highestearningemployeeandit’sname. SELECT Fname,Minit,Lname,salary FROM EMPLOYEE WHERE Salary=(SELECTMAX(salary) FROMemployee WHEREsalary<(SELECT MAX(salary) FROM employee )) Q12.Retrieve alistof the employeeandthe Projectstheyare workingonorderedbydepartmentand withindepartmentorderedbythe alphabeticallybylastname thenfirstname. SELECT Dname ,Lname ,Fname ,Pname FROM EMPLOYEE,DEPARTMENT,PROJECT,WORKS_ON WHERE Dnumber=dnoandssn=essnandpno=Pnumber orderby dname ,Lname,Fname Q10 Max_sal 55000 Q11 Fname Minit Lname salary JenniferS Wallace 43000 Q12 Dname Lname Fname Pname Administration Jabbar Ahmad Newbenefits Administration Jabbar Ahmad Computerization Administration Wallace Jennifer Newbenefits Administration Wallace Jennifer Reorganisation Administration Zelaya Alicia Newbenefits Administration Zelaya Alicia Computerization Headquarters Borg James Reorganisation Research Narayan Ramesh ProductZ Research Smith John ProductY Research Smith John ProductX Research Wong Franklin Reorganisation Research Wong Franklin Computerization Research Wong Franklin Computerization Research Wong Franklin ProductZ Research Wong Franklin ProductY

Mais Conteúdo rRelacionado

Audiolivros relacionados

Gratuito durante 30 dias do Scribd

Ver tudo

×