Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.87 KB | None | 0 0
  1. zadanka z 2-1
  2.  
  3. zad 1
  4. The manager of Global Fast Foods would like to send out coupons for the upcoming sale.
  5. He wants to send one coupon to each household. Create the SELECT statement that returns the customer last name and a mailing address.
  6.  
  7. select distinct last_name, address from f_customers
  8.  
  9.  
  10. zad 3
  11. Sue, Bob, and Monique were the employees of the month. Using the f_staffs table, create a SELECT statement to display the results as shown in the Super Star chart.
  12.  
  13. select ' *** '||first_name||' *** '||first_name||' *** ' "SUPER STAR" from f_staffs
  14.  
  15.  
  16. zad 5
  17. Global Fast Foods has decided to give all staff members a 5% raise. Prepare a report that presents the output as shown in the chart.
  18.  
  19. select last_name "EMPLOYEE LAST NAME", salary "CURRENT SALARY", salary*1.05 "SALARY WITH 5% RAISE" from f_staffs
  20.  
  21.  
  22. zad6
  23. Create a query that will return the structure of the Oracle database EMPLOYEES table. Which columns are marked “nullable”? What does this mean?
  24.  
  25. describe employees
  26.  
  27. zad7
  28. The owners of DJs on Demand would like a report of all items in their D_CDs table with the following
  29. column headings: Inventory Item, CD Title, Music Producer, and Year Purchased. Prepare this report
  30.  
  31. select * from d_cds - bez aliasów
  32.  
  33.  
  34.  
  35.  
  36. zadanka z 2-2
  37.  
  38. zad1
  39. Using the Global Fast Foods database, retrieve the customer’s first name, last name, and address for the customer who uses ID 456.
  40.  
  41. select first_name, last_name, address from f_customers where id=456
  42.  
  43. zad2
  44. Show the name, start date, and end date for Global Fast Foods' promotional item “ballpen and highlighter” giveaway.
  45.  
  46. select name, start_date, end_date from f_promotional_menus where give_away='ballpen and highlighter'
  47.  
  48. zad5
  49. The manager of DJs on Demand would like a report of all the CD titles and years of CDs that were produced before 2000.
  50.  
  51. select title, year from d_cds where year < 2000
  52.  
  53. zad7
  54. Write a SQL statement that will display the student number (studentno),
  55. first name (fname), and last name (lname) for all students who are female (F) in the table named students.
  56. kolumna znajduje sie w poleceniu
  57.  
  58. select studentno, fname, lname from students where sex = 'F'
  59.  
  60. zad10
  61. Write a SQL statement that will list the titles and years of all the DJs on Demand CDs that were not produced in 2000.
  62. select title, year from d_cds where year != 2000
  63.  
  64. zad11
  65. Write a SQL statement that lists the Global Fast Foods employees who were born before 1980.
  66.  
  67. select * from f_staffs where birthdate<'01-Jan-1980'
  68.  
  69.  
  70.  
  71. zadanka 2-3
  72.  
  73. zad1
  74. Display the first name, last name, and salary of all Global Fast Foods staff whose salary is between $5.00 and $10.00 per hour.
  75.  
  76. select first_name, last_name, salary from f_staffs where salary between 5 and 10
  77.  
  78.  
  79. zad4
  80. Create a list of all the DJs on Demand CD titles that have “a” as the second letter in the title.
  81.  
  82. select title from d_cds where title like '_a%'
  83.  
  84. zad5
  85. Who are the partners of DJs on Demand who do not get an authorized expense amount?
  86.  
  87. select * from d_partners where auth_expense_amt is null
  88.  
  89. zad6
  90. Select all the Oracle database employees whose last names end with “s”. Change the heading of the column to read Possible Candidates.
  91.  
  92. select last_name "Possible Candidates" from employees where last_name like '%s'
  93.  
  94. zad8
  95. Write a SQL statement that lists the songs in the DJs on Demand inventory that are type code 77, 12, or 1.
  96.  
  97. select * from d_songs where type_code in (77,12,1)
  98.  
  99.  
  100.  
  101.  
  102. zadanka 3-1
  103.  
  104. zad2
  105. Display the last names of all Global Fast Foods employees who have “e” and “i” in their last names
  106.  
  107. select last_name from f_staffs where last_name like '%e%' and last_name like '%i%'
  108.  
  109. zad3
  110. I need to know who the Global Fast Foods employees are that make more than $6.50/hour and their position is not order taker.
  111.  
  112. select * from f_staffs where salary>6.5 and staff_type!='Order Taker'
  113.  
  114. zad4
  115. Using the employees table, write a query to display all employees whose last names start with “D” and have “a” and “e” anywhere in their last name.
  116.  
  117. select last_name from employees where last_name like 'D%e%' and last_name like 'D%a%'
  118. select last_name from employees where last_name like 'D%e%a%' or last_name like 'D%a%e%'
  119.  
  120.  
  121.  
  122.  
  123. zadanka 3-2
  124.  
  125. zad1
  126. In the example below, assign the employee_id column the alias of “Number.” Complete the SQL statement to order the result set by the column alias.
  127. SELECT employee_id, first_name, last_name
  128. FROM employees;
  129.  
  130. odp:
  131. SELECT employee_id "Number", first_name, last_name
  132. FROM employees
  133. order by "Number"
  134.  
  135. zad2
  136. Create a query that will return all the DJs on Demand CD titles ordered by year with titles in alphabetical order by year
  137.  
  138. select year,title from d_cds order by year, title
  139.  
  140. zad3
  141. Order the DJs on Demand songs by descending title. Use the alias “Our Collection” for the song title
  142.  
  143. select title "Our Collection" from d_songs order by title DESC
  144.  
  145.  
  146.  
  147. zadanka 4-1
  148.  
  149. zad3
  150. What is the length of the string “Oracle Internet Academy”?
  151.  
  152. select length('Oracle Internet Academy') from dual
  153.  
  154. zad4
  155. What’s the position of “I” in “Oracle Internet Academy”?
  156.  
  157. select instr('Oracle Internet Academy','I') from dual
  158.  
  159. zad5
  160. Starting with the string “Oracle Internet Academy”, pad the string to create ****Oracle****Internet****Academy****
  161.  
  162. select rpad(lpad('Oracle',10,'*'),14,'*') || rpad('Internet',12,'*') || rpad('Academy',11,'*') from dual
  163.  
  164. zad9
  165. Write a query that will output a column called “ADDRESS” which has the following information:
  166. ZOE TWEE 1009 OLIVER AVENUE BOSTON, MA 12889. Use the Global Fast Foods F_CUSTOMERS table.
  167.  
  168. select upper(first_name)||' '||upper(last_name)||' '||upper(address)||' '||upper(city)||' '||upper(state)||' '||zip "address" from f_customers
  169.  
  170. zad10
  171. Write a query to return the first character of the first name concatenated to the last_name, the salary, and the department id for employees working in department 20.
  172. Give the first expression an alias of Name. Use the EMPLOYEES table. Change the query to use a substitution variable instead of the hard coded value 20 for department id.
  173. Run the query for department 30 and 50 without changing the original where-clause in your statement
  174.  
  175. select substr(first_name,1,1)||last_name "Name", salary, department_id from employees where department_id = :podaj_dept
  176.  
  177.  
  178.  
  179.  
  180. zadanka 4-2
  181.  
  182. 1. Display Oracle database employee last_name and salary for employee_ids between 100 and 102.
  183. Include a third column that divides each salary by 1.55 and rounds the result to two decimal places.
  184.  
  185. select last_name, salary, round(salary/1.55,2) from employees where employee_id between 100 and 102
  186.  
  187. 2. Display employee last_name and salary for those employees who work in department 80. Give each of them a raise of 5.333% and truncate the result to two decimal places.
  188.  
  189. select last_name, salary, trunc(salary*1.05333,2) from employees where department_id = 80
  190.  
  191. 5. Divide each employee’s salary by 3. Display only those employees’ last names and sala-ries who earn a salary that is a multiple of 3.
  192.  
  193. select last_name, salary, mod(salary,3) from employees where mod(salary,3)=0
  194.  
  195.  
  196.  
  197.  
  198. zadanka 4-3
  199.  
  200. 1. For DJs on Demand, display the number of months between the event_date of the Vigil wedding and today’s date. Round to the nearest month.
  201.  
  202. select round(months_between(sysdate, event_date)) from d_events where name='Vigil wedding'
  203.  
  204. 2. Display the days between the start of last summer’s school vacation break and the day school started this year. Assume 30.5 days per month. Name the output “Days.”
  205.  
  206. select months_between('01-Oct-2019','20-Jun-2019')*30.5 as "Days" from dual
  207.  
  208. 5. What is the last day of the month for June 2005? Use an alias for the output.
  209.  
  210. select last_day('15-Jun-2005') as"LasTDAy" from dual
  211.  
  212. 7. Your next appointment with the dentist is six months from today. On what day will you go to the dentist? Name the output, “Appointment.”
  213.  
  214. select add_months(sysdate,6) as"Appointment." from dual
  215.  
  216. 10. What’s the date of the next Friday after your birthday this year? Name the output, “First Friday.”
  217.  
  218. select next_day(sysdate, 'Friday') as"First Friday." from dual
  219.  
  220.  
  221.  
  222.  
  223. zadanka 5-1
  224.  
  225. 1. List the last names and birthdays of Global Fast Food Employees. Convert the birth dates to character data in the Month DD, YYYY format. Suppress any leading zeros.
  226.  
  227. select last_name, to_char(birthdate,'fmMonth DD, YYYY') from f_staffs
  228.  
  229. 3. Format a query from the Global Fast Foods f_promotional_menus table to print out the start_date of promotional code 110 as: The promotion began on the tenth of February 2004.
  230.  
  231. select 'The promotion began on the '||to_char(start_date,'ddspth "of" Month YYYY') from f_promotional_menus where code=110
  232.  
  233. 5. List the ID, name, and salary for all Global Fast Foods employees. Display salary with a $ sign and two decimal places.
  234.  
  235. select id, last_name, to_char(salary, '$99.99') from f_staffs
  236.  
  237. 8. Create one query that will convert 25-Dec-2004 into each of the following (you will have to convert 25-Dec-2004 to a date and then to character data)
  238. :December 25th, 2004 DECEMBER 25TH, 2004 25th december, 2004
  239.  
  240. select to_char(sysdate, 'Month ddth, YYYY') from dual
  241. select to_char(sysdate, 'MONTH DDTH, YYYY') from dual
  242. select to_char(sysdate, 'ddth month, YYYY') from dual
  243.  
  244.  
  245.  
  246. zadanka 5-2
  247.  
  248. 1. Create a report that shows the Global Fast Foods promotional name, start date, and end date from the f_promotional_menus table.
  249. If there is an end date, temporarily replace it with “end in two weeks.” If there is no end date, replace it with today’s date.
  250.  
  251. select name, start_date, NVL2(end_date, 'end in two weeks', sysdate) from f_promotional_menus
  252.  
  253. 2. Not all Global Fast Foods staff members receive overtime pay. Instead of displaying a null value for these employees, replace null with zero.
  254. Include the employee’s last name and overtime rate in the output. Label the overtime rate as “Overtime Status”.
  255.  
  256. select last_name, overtime_rate, NVL(overtime_rate,0) "Overtime Status" from f_staffs
  257.  
  258. 7.
  259. a) Create a report listing the first and last names and month of hire for all employees in the EMPLOYEES table (use TO_CHAR to convert hire_date to display the month).
  260. b) Modify the report to display null if the month of hire is September. Use the NULLIF function.
  261.  
  262. a) select first_name, last_name, to_char(hire_date, 'Month') from employees
  263. b) select first_name, last_name, NULLIF(to_char(hire_date, 'Month'),'September') from employees
  264.  
  265.  
  266. zadanka 5-3
  267.  
  268. 1. From the DJs on Demand d_songs table, create a query that replaces the 2-minute songs with “shortest” and the 10-minute songs with “longest”. Label the output column “Play Times”.
  269.  
  270. select title, decode(duration,'2 min','shortest','10 min','longest') "Play Times" from d_songs
  271.  
  272. 2. Use the Oracle database employees table and CASE expression to decode the depart-ment id.
  273. Display the department id, last name, salary, and a column called “New Salary” whose value is based on the following conditions:
  274. If the department id is 10 then 1.25 * salary
  275. If the department id is 90 then 1.5 * salary
  276. If the department id is 130 then 1.75 * salary
  277. Otherwise, display the old salary.
  278.  
  279. select last_name, salary,
  280. case department_id
  281. when 10 then 1.25 * salary
  282. when 90 then 1.5 * salary
  283. when 130 then 1.75 * salary
  284. else salary
  285. end "New Salary"
  286. from employees
  287.  
  288. 3. Display the first name, last name, manager ID, and commission percentage of all employ-ees in departments 80 and 90.
  289. In a 5th column called “Review”, again display the manager ID. If they don’t have a manager, display the commission percentage. If they don’t have a commission, display 99999.
  290.  
  291. select first_name, last_name, manager_id, commission_pct, coalesce(manager_id, commission_pct, 99999) "Review" from employees where department_id in (80,90)
  292.  
  293.  
  294. zadanka 12-1
  295.  
  296. 2. DJs on Demand just purchased four new CDs. Use an explicit INSERT statement to add each CD to the copy_d_cds table.
  297. After completing the entries, execute a SELECT * statement to verify your work.
  298.  
  299. create table copy_d_cds as select * from d_cds
  300. insert into copy_d_cds values (97, 'Celebrate the Day','R&B Inc.','2003')
  301. insert into copy_d_cds values (98, 'Holiday Tunes for All Ages','Tunes are Us','2004')
  302. insert into copy_d_cds values (99, 'Party Music','Old Town Records','2004')
  303. insert into copy_d_cds values (100, 'Best of Rock and Roll','Old Town Records','2004')
  304.  
  305. 3. DJs on Demand has two new events coming up. One event is a fall football party and the other event is a sixties theme party.
  306. The DJs on Demand clients requested the songs shown in the table for their events. Add these songs to the copy_d_songs table using an implicit INSERT statement.
  307.  
  308. create table copy_d_songs as select * from d_songs
  309. insert into copy_d_songs values(52,'Surfing Summer','Not known',null,12)
  310. insert into copy_d_songs (ID, Title, Duration, Type_Code) values(53,'Victory Victory','5 min',12) drugi sposob bez null
  311.  
  312. 6.
  313. Create a table called rep_email using the following statement:
  314. CREATE TABLE rep_email (id NUMBER(2) CONSTRAINT rel_id_pk PRIMARY KEY,first_name VARCHAR2(10),last_name VARCHAR2(10),email_address VARCHAR2(10))
  315.  
  316. insert into rep_email
  317. select employee_id, first_name, last_name, email from employees where job_id like '%REP%'
  318. select employee_id, first_name, last_name, email from employees where job_id like '%REP%'
  319.  
  320.  
  321. zadanka 12-2
  322.  
  323. 1. Monique Tuttle, the manager of Global Fast Foods, sent a memo requesting an immediate change in prices. The price for a strawberry shake will be raised from $3.59 to $3.75, and the price for fries will increase to $1.20. Make these changes to the copy_f_food_items ta-ble.
  324.  
  325. create table copy_f_food_items as select * from f_food_items
  326.  
  327. update copy_f_food_items
  328. set price=3.75
  329. where description='Strawberry Shake'
  330.  
  331. update copy_f_food_items
  332. set price=1.20
  333. where description='Fries'
  334.  
  335. 2. Bob Miller and Sue Doe have been outstanding employees at Global Fast Foods. Man-agement has decided to reward them by increasing their overtime pay. Bob Miller will re-ceive an additional $0.75 per hour and Sue Doe will receive an additional $0.85 per hour. Update the copy_f_staffs table to show these new values. (Note: Bob Miller cur-rently doesn’t get overtime pay. What function do you need to use to convert a null value to 0?)
  336.  
  337. create table copy_f_staffs as select * from f_staffs
  338.  
  339. update copy_f_staffs
  340. set overtime_rate=nvl(overtime_rate,0)+0.75
  341. where first_name='Bob' and last_name='Miller'
  342.  
  343. update copy_f_staffs
  344. set overtime_rate=nvl(overtime_rate,0)+0.85
  345. where first_name='Sue' and last_name='Doe'
  346.  
  347. 5. Sue Doe has been an outstanding Global Foods staff member and has been given a sal-ary raise. She will now be paid the same as Bob Miller. Update her record in copy_f_staffs.
  348.  
  349. update copy_f_staffs
  350. set salary=(select salary from copy_f_staffs where first_name='Bob' and last_name='Miller')
  351. where first_name='Sue' and last_name='Doe'
  352.  
  353. 8. Execute the following SQL statement. Record your results.
  354. DELETE from departments
  355. WHERE department_id = 60;
  356.  
  357. 9. Kim Kai has decided to go back to college and does not have the time to work and go to school. Delete him from the Global Fast Foods staff. Verify that the change was made.
  358.  
  359. DELETE from copy_f_staffs
  360. where first_name='Kim' and last_name='Kai'
  361.  
  362.  
  363. zadanka 12-3 tego na kartkowce niue bedzie w sensie ze insert all
  364.  
  365. 2. Currently, the Global Foods F_PROMOTIONAL_MENUS table START_DATE column does not have SYSDATE set as DEFAULT. Your manager has decided she would like to be able to set the starting date of promotions to the current day for some entries. This will require three steps:
  366. a. In your schema, Make a copy of the Global Foods F_PROMOTIONAL_MENUS table using the following SQL statement:
  367. CREATE TABLE copy_f_promotional_menus
  368. AS (SELECT * FROM f_promotional_menus)
  369. b. Alter the current START_DATE column attributes using:
  370. ALTER TABLE copy_f_promotional_menus
  371. MODIFY(start_date DATE DEFAULT SYSDATE)
  372. c. INSERT the new information and check to verify the results.INSERT a new row into the copy_f_promotional_menus table for the manager’s new promotion. The promotion code is 120. The name of the promotion is ‘New Customer.’ Enter DEFAULT for the start date and '01-Jun-2005' for the ending date. The giveaway is a 10% discount coupon. What was the correct syntax used?
  373.  
  374. CREATE TABLE copy_f_promotional_menus
  375. AS (SELECT * FROM f_promotional_menus)
  376. -----------------------------------
  377. ALTER TABLE copy_f_promotional_menus
  378. MODIFY(start_date DATE DEFAULT SYSDATE)
  379. ----------------------------------------
  380. insert into copy_f_promotional_menus
  381. values(120,'New Customer',default,'01-Jun-2005','10% discount coupon')
  382. ------------------------------------------
  383. select * from copy_f_promotional_menus
  384.  
  385. 4. Run the following 3 statements to create 3 new tables for use in a Multi-table insert state-ment. All 3 tables should be empty on creation, hence the WHERE 1=2 condition in the WHERE clause.
  386.  
  387. tworze podane tabele
  388.  
  389. Once the tables exist in your account, write a Multi-Table insert statement to first select the employee_id, hire_date, salary, and manager_id of all employees. If the salary is more than 20000 insert the employee_id and salary into the special_sal table. Insert the details of employee_id, hire_date, and salary into the sal_history table. Insert the em-ployee_id, manager_id, and salary into the mgr_history table.
  390.  
  391. insert all
  392. when salary>20000 then
  393. into special_sal values (employee_id,salary)
  394. else
  395. into sal_history values (employee_id, hire_date, salary)
  396. into mgr_history values (employee_id, manager_id, salary)
  397. select employee_id, hire_date, salary, manager_id from employees
  398.  
  399.  
  400.  
  401. zadanka 13-1
  402.  
  403. 2. Write the syntax to create the grad_candidates table.
  404.  
  405. create table GRADUATE_CANDIDATE (student_id number(6), last_name varchar2(50), first_name varchar2(50),credits number(3),graduation_date date)
  406.  
  407. 4. Create a new table using a subquery(podzapytania). Name the new table your last name -- e.g., smith_table. Using a subquery, copy grad_candidates into smith_table.
  408.  
  409. create table smith_table as
  410. select * from GRADUATE_CANDIDATE
  411.  
  412. 6. Query the data dictionary for each of the following: •USER_TABLES •USER_OBJECTS •USER_CATALOG or USER_CAT In separate sentences, summarize what each query will return
  413.  
  414. select * from USER_TABLES
  415. select * from USER_OBJECTS
  416. select * from USER_CATALOG
  417.  
  418.  
  419.  
  420. zadanka 13-3
  421.  
  422. 3. In your o_employees table, enter a new column called “Termination.” The datatype for the new column should be VARCHAR2. Set the DEFAULT for this column as SYSDATE to appear as character data in the format: February 20th, 2003
  423. modyfikacja istniejacej tabeli na kartkowce bedzie (polecenia: alter, drop, create)
  424. alter table o_employees
  425. add (Termination VARCHAR2(50) default to_char(sysdate,'MONTH ddth, yyyy'))
  426.  
  427. 5. Truncate the o_jobs table. Then do a SELECT * statement. Are the columns still there? Is the data still there?
  428.  
  429. select * from o_jobs
  430. truncate table o_jobs
  431. select * from o_jobs
  432.  
  433. 9. Rename the o_jobs table to o_job_description
  434.  
  435. rename o_jobs to o_jobs_description
  436. select * from o_jobs_description
  437.  
  438. 10. F_staffs table exercises:
  439. a. Create a copy of the f_staffs table called copy_f_staffs and use this copy table for the remain-ing labs in this lesson.
  440. b. Describe the new table to make sure it exists.
  441. c. Drop the table.
  442. d. Try to select from the table.
  443. e. Investigate your recyclebin to see where the table went.
  444. f. Try to select from the dropped table by using the value stored in the OBJECT_NAME column. You will need to copy and paste the name as it is exactly, and enclose the new name in “ “ (double quotes). So if the dropped name returned to you is BIN$Q+x1nJdcUnngQESYELVIdQ==$0, you need to write a query that refers to “BIN$Q+x1nJdcUnngQESYELVIdQ==$0”.
  445. g. Undrop the table.
  446. h. Describe the table.
  447.  
  448. create table copy_f_staffs
  449. as select * from f_staffs
  450.  
  451. drop table copy_f_staffs
  452. select * from copy_f_staffs
  453. zobaczyc czy jest w koszu select * from user_recyclebin
  454.  
  455. flashback table copy_f_staffs to before drop
  456. select * from copy_f_staffs
  457.  
  458. drop table copy_f_staffs purge (usuniece na stałe bez kosza, juz nie przywrócimy tabeli)
  459.  
  460. !!bedzie zadanie na kartkowce z constraintsami(wiezy integralności, na egz tez bedzie jest ich 5)!!
  461.  
  462.  
  463.  
  464. zadanka 14-1
  465.  
  466. 6. Write the CREATE TABLE statement for the Global Fast Foods locations table to define the constraints at the column level.
  467.  
  468. create table Global_Fast_Foods_Locations(
  469. id NUMBER primary key,
  470. name varchar2(50),
  471. date_opened DATE NOT NULL,
  472. address varchar2(50)NOT NULL,
  473. city varchar2(50)NOT NULL,
  474. zip varchar2(50),
  475. phone varchar2(50),
  476. email varchar2(50) UNIQUE,
  477. manager_id NUMBER,
  478. emergency_contact varchar2(50))
  479.  
  480. 9. Rewrite the CREATE TABLE statement for the Global Fast Foods locations table to define the UNIQUE constraints at the table level. Do not execute this statement.
  481.  
  482. create table Global_Fast_Foods_Locations(
  483. id NUMBER primary key,
  484. name varchar2(50),
  485. date_opened DATE NOT NULL,
  486. address varchar2(50)NOT NULL,
  487. city varchar2(50)NOT NULL,
  488. zip varchar2(50),
  489. phone varchar2(50),
  490. email varchar2(50),
  491. manager_id NUMBER,
  492. emergency_contact varchar2(50),
  493. CONSTRAINT email_uk UNIQUE(email)
  494. )
  495.  
  496.  
  497.  
  498.  
  499. zadanka 14-2
  500.  
  501. każda tabela powinnna miec klucz główny, nie moze byc nullem
  502.  
  503. 3. Create the animals table. Write the syntax you will use to create the table.
  504. create table animals(
  505. animal_id NUMBER(6) PRIMARY KEY,
  506. name VARCHAR2(25),
  507. license_tag_number NUMBER(10) UNIQUE,
  508. admit_date DATE NOT NULL,
  509. adoption_id NUMBER(5) NOT NULL,
  510. vaccination_date DATE)
  511.  
  512. 4. Enter one row into the table. Execute a SELECT * statement to verify your input. Refer to the graphic below for input
  513.  
  514. insert into animals values (101, 'Spot', 35540, '10-Oct-2004', 205, '12-Oct-2004')
  515.  
  516. 5. Write the syntax to create a foreign key (adoption_id) in the animals table that has a corresponding primary- key reference in the adoptions table. Show both the column-level and table-level syntax. Note that because you have not actually created an adoptions table, no adoption_id primary key exists, so the foreign key cannot be added to the animals table.
  517.  
  518. create table animals(
  519. animal_id NUMBER(6) PRIMARY KEY,
  520. name VARCHAR2(25),
  521. license_tag_number NUMBER(10) UNIQUE,
  522. admit_date DATE NOT NULL,
  523. adoption_id NUMBER(5) REFERENCES adoptions(adoption_id),
  524. vaccination_date DATE NOT NULL)
  525.  
  526.  
  527. ZADANIE DODOTKOWE do 14-2 wiezy integralności
  528.  
  529. tworzymy 2 tablele ksiazka i wydawnictwo i nadjemy im pewne uprawnienia
  530.  
  531. create table wydawnictwo(
  532. id_wyd NUMBER PRIMARY KEY,
  533. rok NUMBER CHECK (rok like '____'))
  534.  
  535. create table ksiazka(
  536. id NUMBER PRIMARY KEY,
  537. tytul VARCHAR2(50) NOT NULL,
  538. cena NUMBER CHECK (cena>0),
  539. id_wyd NUMBER REFERENCES wydawnictwo(id_wyd))
  540.  
  541.  
  542.  
  543. zadanka 14-3
  544.  
  545. 2. Since the tables are copies of the original tables, the integrity rules are not passed onto the new tables; only the column datatype definitions remain. You will need to add a PRIMARY KEY constraint to the copy_d_clients table. Name the primary key copy_d_clients_pk . What is the syntax you used to create the PRIMARY KEY constraint to the copy_d_clients.table?
  546.  
  547. create table copy_d_clients as select * from d_clients
  548.  
  549. alter table copy_d_clients
  550. add constraint copy_d_clients_pk PRIMARY KEY(client_number)
  551.  
  552. 3. Create a FOREIGN KEY constraint in the copy_d_events table. Name the foreign key copy_d_events_fk. This key references the copy_d_clients table client_number column. What is the syntax you used to create the FOREIGN KEY constraint in the copy_d_events table?
  553.  
  554. create table copy_d_events as select * from d_events
  555.  
  556. alter table copy_d_events
  557. add constraint nnnnnn FOREIGN KEY(client_number) references copy_d_clients(client_number)
  558.  
  559.  
  560.  
  561. ----------------------------------------------------------------------------------------------
  562. 16.12.2019 Kartkówka z działów 12,13,14
  563. Insert, update, delate
  564. create table, alter table
  565. foreign, primary key, check, not unique, not null
  566. ----------------------------------------------------------------------------------------------
  567.  
  568.  
  569.  
  570. zadanka 6-1
  571.  
  572. 1. Create a cross-join that displays the last name and department name from the employees and de-partments tables.
  573.  
  574. select last_name, department_name
  575. from employees cross join departments
  576.  
  577. 3. Create a query that uses a natural join to join the departments table and the locations table. Re-strict the output to only department IDs of 20 and 50. Display the department id, department name, location id, and city.
  578.  
  579. select department_id, department_name, location_id, city
  580. from departments natural join locations
  581.  
  582.  
  583.  
  584.  
  585.  
  586. zadanka 6-2
  587.  
  588. 1. Join the Oracle database locations and departments table using the location_id column. Limit the results to location 1400 only.
  589.  
  590. select * from departments d join locations l on (d.location_id = l.location_id)
  591. where d.location_id=1400
  592.  
  593. 2. Join DJs on Demand d_play_list_items, d_track_listings, and d_cds tables with the JOIN USING syntax. Include the song ID, CD number, title, and comments in the output.
  594.  
  595. select song_ID, CD_number, title, comments
  596. from d_cds join d_track_listings using(CD_number) join d_play_list_items using(song_ID)
  597.  
  598. 3. Display the city, department name, location ID, and department ID for departments 10, 20, and 30 for the city of Seattle.
  599.  
  600. select city, department_name, d.location_id, department_id
  601. from departments d join locations l on (d.location_id = l.location_id)
  602. where city='Seattle' and department_id in (10,20,30)
  603.  
  604. 5. Write a statement joining the employees and jobs tables. Display the first and last names, hire date, job id, job title, and maximum salary. Limit the query to those employees who are in jobs that can earn more than $12,000.
  605.  
  606. select first_name, last_name, hire_date, e.job_id, job_title, max_salary
  607. from employees e join jobs j on (e.job_id = j.job_id)
  608. where salary > 12000
  609.  
  610. 7. Write a statement that displays the employee ID, first name, last name, manager ID, man-ager first name, and manager last name for every employee in the employees table. Hint: this is a self-join.
  611.  
  612. pomijamy te zadanie narazie
  613. robimy
  614. select w.last_name, m.last_name from employees w join employees m on (w.manager_id=m.employee_id)
  615.  
  616. 8. Use JOIN ON syntax to query and display the location ID, city, and department name for all Canadian locations.
  617.  
  618. select d.location_id, city, department_name
  619. from departments d join locations l on (d.location_id = l.location_id) join countries c on (l.country_id = c.country_id)
  620. where country_name='Canada'
  621.  
  622. 9. Query and display manager ID, department ID, department name, first name, and last name for all employees in departments 80, 90, 110, and 190
  623.  
  624. select e.manager_ID, e.department_id, department_name, first_name, last_name from employees e join departments d on (e.department_id=d.department_id)
  625. where d.department_id in (80,90,110,190)
  626.  
  627. 10. Display employee ID, last name, department ID, department name, and hire date for those employees whose hire date was June 7, 1994.
  628.  
  629. select e.employee_id, e.department_id, department_name, hire_date, last_name from employees e join departments d on (e.department_id=d.department_id)
  630. where hire_date ='07-Jun-1994'
  631.  
  632.  
  633.  
  634. zadanka 6-3
  635.  
  636. 1. Return the first name, last name, and department name for all employees including those employees not assigned to a department.
  637.  
  638. select first_name, last_name, department_name from employees e left join departments d on (e.department_id = d.department_id)
  639.  
  640. 2. Return the first name, last name, and department name for all employees including those departments that do not have an employee assigned to them.
  641.  
  642. select first_name, last_name, department_name from employees e right join departments d on (e.department_id = d.department_id)
  643.  
  644. 3. Return the first name, last name, and department name for all employees including those departments that do not have an employee assigned to them and those employees not assigned to a department.
  645.  
  646. select first_name, last_name, department_name from employees e full join departments d on (e.department_id = d.department_id)
  647.  
  648. 4. Create a query of the DJs on Demand database to return the first name, last name, event date, and description of the event the client held. Include all the clients even if they have not had an event scheduled.
  649.  
  650. select first_name, last_name, event_date, description from d_clients c left join d_events e on (c.client_number=e.client_number)
  651.  
  652.  
  653.  
  654. zadanka 8-1
  655.  
  656. 2. Create a query that will show the average cost of the DJs on Demand events. Round to two decimal places.
  657.  
  658. select round(avg(cost),2) from d_events
  659.  
  660. 4. Find the sum of the salaries for Global Fast Foods staff members whose IDs are 12 and 9.
  661.  
  662. select sum(salary) from f_staffs where id in (9,12)
  663.  
  664. 5. Using the Oracle database, select the lowest salary, the most recent hire date, the last name of the person who is at the top of an alphabetical list of employees, and the last name of the person who is at the bottom of an alphabetical list of employees. Select only employees who are in departments 50 or 60.
  665.  
  666. select min(salary), max(hire_date), min(last_name), max(last_name) from employees where department_id in (50,60)
  667.  
  668. 9. Create a query that will return the average order total for all Global Fast Foods orders fromJanuary 1, 2002, to December 21, 2002.
  669.  
  670. select avg(order_total) from f_orders where order_date between '01-Jan-2002' and '21-Dec-2002'
  671.  
  672.  
  673.  
  674. zadanka 8-2
  675.  
  676. 1. How many songs are listed in the DJs on Demand D_SONGS table?
  677.  
  678. select count(*) from d_songs
  679.  
  680. 2. In how many different location types has DJs on Demand had venues?
  681.  
  682. select count(distinct loc_type) from d_venues distinct bierze tylko rozne lokalizacje
  683.  
  684. 4. How many of the DJs on Demand customers have email addresses?
  685.  
  686. select count(email) from d_clients
  687.  
  688. 7. Create a query that will convert any null values in the auth_expense_amt column on the DJs on Demand D_PARTNERS table to 100000 and find the average of the values in this column. Round the result to two decimal places.
  689.  
  690. select round(avg(nvl(auth_expense_amt, 100000))) from d_partners
  691.  
  692.  
  693.  
  694. zadanka 9-1
  695.  
  696. 2. Each of the following SQL queries has an error. Find the error and correct it. Use Oracle Application Express to verify that your corrections produce the desired results.
  697.  
  698. select min(avg(salary)), max(avg(salary))
  699. from employees
  700. group by department_id;
  701.  
  702. 5. Write a query that will return both the maximum and minimum average salary grouped by department from the employees table.
  703.  
  704. select avg(max(salary))
  705. from employees
  706. group by department_id;
  707.  
  708.  
  709. zadania z maila do 9.1
  710.  
  711. 1.
  712. select manager_id, job_id, count(*)
  713. from employees
  714. group by manager_id, job_id
  715. order by manager_id;
  716.  
  717. 2.
  718. select job_id, avg(salary)
  719. from employees
  720. group by job_id
  721. having count(*)>3
  722.  
  723. 3.
  724. select job_id, count(*)
  725. from employees
  726. where manager_id<>101
  727. group by job_id
  728. having avg(salary)>3000
  729.  
  730. 4.
  731. select max(salary)-min(salary) from employees
  732.  
  733. 5.
  734. select manager_id, count(manager_id)
  735. from employees
  736. where manager_id is not null
  737. group by manager_id
  738.  
  739. 6.
  740. select department_id
  741. from employees
  742. group by department_id
  743. having sum(nvl(commission_pct,0))=0
  744.  
  745.  
  746.  
  747.  
  748. zadanka 9-3
  749.  
  750. 2. Write one query to return the employee_id, job_id, hire_date, and department_id of all employees and a second query listing employee_id, job_id, start_date, and department_id from the job_history table and combine the results as one single output. Make sure you suppress duplicates in the output.
  751.  
  752. select employee_id, job_id, hire_date, department_id from employees
  753. union
  754. select employee_id, job_id, start_date, department_id from job_history
  755.  
  756. 3. Amend the previous statement to not suppress duplicates and examine the output. How many extra rows did you get returned and which were they? Sort the output by employ-ee_id to make it easier to spot.
  757.  
  758. select employee_id, job_id, hire_date, department_id from employees
  759. union all
  760. select employee_id, job_id, start_date, department_id from job_history
  761. order by employee_id
  762.  
  763. 4. List all employees who have not changed jobs even once. (Such employees are not found in the job_history table)
  764.  
  765. select employee_id from employees
  766. minus
  767. select employee_id from job_history
  768.  
  769. 5. List the employees that HAVE changed their jobs at least once.
  770.  
  771. select employee_id from employees
  772. intersect
  773. select employee_id from job_history
  774.  
  775.  
  776.  
  777.  
  778. zadanka 10-1
  779.  
  780. 3. What DJs on Demand d_play_list_items song_id’s have the same event_id as song_id 45?
  781.  
  782. select song_id from d_play_list_items where event_id=(select event_id from d_play_list_items where song_id=45)
  783.  
  784. 4. Which events in the DJs on Demand database cost more than event_id = 100?
  785.  
  786. select id from d_events where cost>(select cost from d_events where id=100)
  787.  
  788. 8. What are the names of the Global Fast Foods staff members whose staff types are not the same as Bob Miller’s?
  789.  
  790. select last_name from f_staffs where staff_type!=(select staff_type from f_staffs where last_name='Miller')
  791.  
  792. natatka -> jak mam where staff... to pozniej w nawiasie select (to samo zawsze) czyli staff...
  793.  
  794.  
  795.  
  796.  
  797. zadanka 10-2
  798.  
  799. 1. Write a query to return all those employees who have a salary greater than that of Lorentz and are in the same department as Abel.
  800.  
  801. select * from employees where salary>(select salary from employees where last_name='Lorentz')
  802. and
  803. department_id=(select department_id from employees where last_name='Abel')
  804.  
  805. tutaj tak samo jak wczesniej, czyli jak jest salary to w podzapytaniu tez musi byc salary.
  806.  
  807. 4. What is the staff type for those Global Fast Foods jobs that have a salary less than those of any Cook staff-type jobs?
  808.  
  809. select staff_type from f_staffs where salary>(select min(salary) from f_staffs where staff_type='Cook')
  810.  
  811. 5. Write a query to return a list of department id’s and average salaries where the depart-ment’s average salary is greater than Ernst’s salary.
  812.  
  813. select department_id, avg(salary) from employees
  814. group by department_id
  815. having avg(salary)>(select salary from employees where last_name='Ernst')
  816.  
  817. 6. Return the department ID and minimum salary of all employees, grouped by department ID, having a minimum salary greater than the minimum salary of those employees whose department ID is not equal to 50.
  818.  
  819. select department_id, min(salary) from employees
  820. group by department_id
  821. having min(salary)>(select min(salary) from employees where department_id<>50)
  822.  
  823.  
  824.  
  825.  
  826. zadanka 10-3
  827.  
  828. 2. Write a query that returns jazz and pop songs. Write a multi-row subquery and use the d_songs and d_types tables. Include the id, title, duration, and the artist name.
  829.  
  830. select id, title, duration, artist from d_songs where TYPE_CODE in (select code from d_types where description in ('Jazz', 'Pop'))
  831.  
  832. 3. Find the last names of all employees whose salaries are the same as the minimum salary for any department.
  833.  
  834. select last_name from employees where salary=ANY(select min(salary) from employees group by department_id)
  835.  
  836. 4. Which Global Fast Foods employee earns the lowest salary? Hint: You can use either a single-row or a multiple-row subquery.
  837.  
  838. single-row
  839. select last_name from employees where salary=(select min(salary) from employees)
  840.  
  841. multi-row
  842. select last_name from employees where salary<=ALL(select salary from employees)
  843.  
  844. 9. Write a pair-wise subquery listing the last_name, first_name, department_id, and manager_id for all employees that have the same department_ id and manager_id as em-ployee 141. Exclude employee 141 from the result set.
  845.  
  846. select last_name, first_name, department_id, manager_id from employees where(department_id,manager_id) in (select department_id,manager_id from employees where employee_id=141)
  847. and employee_id!=141
  848.  
  849.  
  850.  
  851.  
  852. zadanka 10-4
  853.  
  854. 2. Write a query that lists the highest earners for each department. Include the last_name, department_id, and the salary for each employee.
  855.  
  856. select last_name, department_id, salary from employees o
  857. where salary=(select max(salary) from employees i where i.department_id=o.department_id)
  858.  
  859. 3. Examine the following select statement and finish it so that it will return the last_name, de-partment_id, and salary of employees who have at least one person reporting to them. So we are effectively looking for managers only. In the partially written SELECT statement, the WHERE clause will work as it is. It is simply testing for the existence of a row in the subquery.
  860. SELECT (enter columns here)
  861. FROM (enter table name here) outer
  862. WHERE 'x' IN (SELECT 'x'
  863. FROM (enter table name here) inner
  864. WHERE inner(enter column name here) = inner(enter column name here)
  865. Finish off the statement by sorting the rows on the department_id column.
  866.  
  867. SELECT last_name, department_id, salary
  868. FROM employees outer
  869. WHERE 'x' IN (SELECT 'x'
  870. FROM employees inner
  871. WHERE inner.manager_id = outer.employee_id)
  872.  
  873.  
  874. 27.01.2020 ostatnia kartkowa z dzialow 6,8,9,10
  875. złaczenia, złaczenia zewnetrze, grupowanie z hevingiem min max, podzapytanie, podzapytanie skorelowane
  876. mozna pocwiczyc dział 11 sobie
  877.  
  878. 04.02.2020 oddajemy projekty we wtorek
  879.  
  880. diagram, wymagania co wyslane byly kiedys
  881. pozniej wyslac na maila opis slowny, diagram i cały kodzik
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement