Advertisement
SX514LEFV

psql exercises

Apr 9th, 2025
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DO $$
  2. DECLARE
  3. recordCount INTEGER;
  4. totalCount INTEGER;
  5. BEGIN
  6. SELECT COUNT(*) INTO recordCount FROM jobs;
  7. RAISE INFO 'Number of records in jobs table: %', recordCount;
  8. totalCount = recordCount;
  9. SELECT COUNT(*) INTO recordCount from categories;
  10. RAISE INFO 'Number of records in categories table: %', recordCount;
  11. totalCount = totalCount + recordCount;
  12. SELECT COUNT(*) INTO recordCount from trailers;
  13. RAISE INFO 'Number of records in trailers table: %', recordCount;
  14. totalCount = totalCount + recordCount;
  15. RAISE INFO 'Total number of records: %', totalCount;
  16. END
  17. $$;
  18.  
  19. DO $$
  20. DECLARE
  21. name VARCHAR(255);
  22. age INTEGER;
  23. person RECORD;
  24. BEGIN
  25. FOR person IN SELECT * FROM people ORDER by id LIMIT 130 LOOP
  26.    age = (CURRENT_DATE-person.birthday)/365;
  27.    name = person.name;
  28.    IF person.birthday IS NOT NULL THEN
  29.     CASE
  30.     WHEN person.deathday IS NOT NULL THEN
  31.         RAISE INFO '% was alive for % years', person.name, age;
  32.     ELSE
  33.         RAISE INFO '% is alive for % years', person.name, age;
  34.     END CASE;
  35.    END IF;
  36. END LOOP;
  37. END
  38. $$;
  39.  
  40. DO $$
  41. DECLARE
  42. movie RECORD;
  43. name VARCHAR(255);
  44. theCount INTEGER = 0;
  45. itCount INTEGER = 0;
  46. anCount INTEGER = 0;
  47. otherCount INTEGER = 0;
  48. BEGIN
  49. FOR movie IN SELECT * FROM movies WHERE kind = 'movie'
  50. LOOP
  51.     CASE
  52.     WHEN STRPOS(movie.name, 'The ') = 1 OR movie.name = 'The' THEN
  53.         theCount = theCount + 1;
  54.     WHEN STRPOS(movie.name, 'It ') = 1 OR movie.name = 'It' THEN
  55.         itCount = itCount + 1;
  56.     WHEN STRPOS(movie.name, 'A ') = 1 OR movie.name = 'A' OR STRPOS(movie.name, 'An ') = 1 OR movie.name = 'An' THEN
  57.         anCount = anCount + 1;
  58.     ELSE
  59.         otherCount = otherCount + 1;
  60.     END CASE;
  61. END LOOP;
  62. RAISE INFO 'Movies starting with The: %', theCount;
  63. RAISE INFO 'Movies starting with It: %', itCount;
  64. RAISE INFO 'Movies starting with A or An: %', anCount;
  65. RAISE INFO 'Movies starting with other words: %', otherCount;
  66. END
  67. $$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement