CREATE TABLE students(id serial, name text, quality1 text, quality2 text);
INSERT INTO students(id, name, quality1, quality2) VALUES
(1,'Bennie Hodkiewicz','brave','cunning'),
(2,'Octavia Huels','lazy','hopeless'),
(3,'Elta Okuneva','materialistic','generous'),
(4,'Mr. Destin Murazik','generous','hopeless'),
(5,'Shaniya Hane','happy','hyper'),
(6,'Abbie Ratke','happy','sad'),
(7,'Mrs. Michelle Lubowitz','generous','lazy'),
(8,'Kassandra Emmerich PhD','flighty','hufflepuff'),
(9,'Mallie Kautzer','shy','sad'),
(10,'Nichole Swaniawski','daring','materialistic'),
(11,'Modesta Ritchie','lazy','flighty'),
(12,'Alena Runte','daring','shy'),
(13,'Dr. Aurelia Wisozk','studious','hyper'),
(14,'Aubrey Larkin I','hufflepuff','cowardly'),
(15,'Miss Jarrell Zulauf','imaginative','sad'),
(16,'Craig Stroman DDS','evil','imaginative'),
(17,'Erica Weissnat','hyper','daring'),
(18,'Miss Clementina Kihn','sad','hyper'),
(19,'Christopher Hilpert','complacent','cowardly'),
(20,'Mrs. Aileen Stark','happy','hopeless'),
(21,'Nikolas Davis','daring','complacent'),
(22,'Claud OKeefe','courageous','materialistic'),
(23,'Claudie Fahey DVM','hufflepuff','imaginative'),
(24,'Berenice Casper','cowardly','courageous'),
(25,'Rolando Olson','imaginative','naive'),
(26,'Wilber Stark','hyper','materialistic'),
(27,'Geovanni Stamm','imaginative','cowardly'),
(28,'Lennie Hodkiewicz','hufflepuff','naive'),
(29,'Destinee Cummings','hufflepuff','flighty'),
(30,'Liliane Cruickshank','brave','lonely'),
(31,'Ms. Katherine Boyer','studious','courageous'),
(32,'Freddie Torp','brave','shy'),
(33,'Mr. Laury Bins','courageous','devious'),
(34,'Brooke Bechtelar','daring','materialistic'),
(35,'Dr. Shana Schuster','imaginative','evil'),
(36,'Oral Gleichner','lazy','intelligent'),
(37,'Sheridan Reynolds','hyper','complacent'),
(38,'Ms. Evelyn Keebler','generous','cunning'),
(39,'Junior Stark','complacent','flighty'),
(40,'Duane Fahey Sr.','materialistic','daring'),
(41,'Ophelia Hilll MD','daring','courageous'),
(42,'Hobart Streich','shy','generous'),
(43,'Stanley Mohr','daring','lonely'),
(44,'Deon Stokes','imaginative','complacent'),
(45,'Garth Schneider','hyper','courageous'),
(46,'Dejon Waters DDS','materialistic','lazy'),
(47,'Miss Elva Feeney','imaginative','naive'),
(48,'Regan Howe','naive','intelligent'),
(49,'Marc Homenick','hyper','complacent'),
(50,'Lenore Trantow','materialistic','evil'),
(51,'Emmet Cruickshank','flighty','shy'),
(52,'Ben Tremblay','imaginative','evil'),
(53,'Madie Bergnaum V','hyper','complacent'),
(54,'Eladio Wehner','brave','lazy'),
(55,'Omer Conroy III','daring','naive'),
(56,'Helen Lind','hufflepuff','courageous'),
(57,'Mrs. Johnathon Hettinger','studious','evil'),
(58,'Ms. Ardella Rosenbaum','naive','complacent'),
(59,'Shany Kohler','flighty','imaginative'),
(60,'Abbey Spinka','lazy','cunning'),
(61,'Haylee Powlowski','generous','happy'),
(62,'Rosario Schulist PhD','happy','courageous'),
(63,'Dovie Torphy','sad','happy'),
(64,'Otis Boyer','happy','sad'),
(65,'Johann Brekke','complacent','materialistic'),
(66,'Ernest Willms','materialistic','hufflepuff'),
(67,'Terrell Oberbrunner','complacent','happy'),
(68,'Ottis Romaguera','evil','devious'),
(69,'Alysha Volkman','lazy','hufflepuff'),
(70,'Leonardo Aufderhar','cowardly','generous'),
(71,'Carolyne Leffler II','evil','complacent'),
(72,'Mrs. Eve Carter','hyper','sad'),
(73,'Loren Steuber','cowardly','sad'),
(74,'Arianna Wiegand','courageous','daring'),
(75,'Miss Sid Little','studious','happy'),
(76,'Hermina Rowe','generous','happy'),
(77,'Maudie Keebler','sad','devious'),
(78,'Julia Ferry','lonely','sad'),
(79,'Kristofer Bartoletti','studious','naive'),
(80,'Amalia Zboncak','happy','evil'),
(81,'Jo Bernier','evil','daring'),
(82,'Zola Ernser DDS','complacent','materialistic'),
(83,'Santina Larson','naive','complacent'),
(84,'Oda Pfeffer','studious','hyper'),
(85,'Sidney Koch','daring','shy'),
(86,'Mrs. Adeline Ryan','complacent','naive'),
(87,'Haylee Kautzer','evil','materialistic'),
(88,'Emanuel Bins','generous','flighty'),
(89,'Gerhard Ziemann','shy','hyper'),
(90,'Saul Rolfson','complacent','courageous'),
(91,'Billy Haag','happy','devious'),
(92,'Evie Beier PhD','evil','lazy'),
(93,'Gudrun Mitchell','devious','naive'),
(94,'Pierce Batz V','lonely','sad'),
(95,'Walton Shanahan','devious','cowardly'),
(96,'Miss Patience Gutkowski','happy','materialistic'),
(97,'Rozella Johnson','evil','cowardly'),
(98,'Dr. Rod Conn','brave','courageous'),
(99,'Trevor Kilback','cowardly','cowardly'),
(100,'Raheem Grady','complacent','hyper');

-- 1
Select name, quality1, quality2,
  Case
	  When quality1 = 'evil' and quality2 = 'cunning' Then 'Slytherin'
    When quality1 = 'brave' and not quality2 = 'evil' Then 'Gryffindor'
    When quality1 = 'studious' or quality2 = 'intelligent' Then 'Ravenclaw'
    When quality1 = 'hufflepuff' or quality2 = 'hufflepuff' Then 'Hufflepuff'
    else 'no suitable'
  End faculty
From students;

--2
CREATE TABLE hogwarts_students(name text, quality1 text, quality2 text, faculty text, total_points int default 0);
INSERT INTO hogwarts_students(
  WITH tmp AS(
    Select name, quality1, quality2,
    Case
  	  When quality1 = 'evil' and quality2 = 'cunning' Then 'Slytherin'
      When quality1 = 'brave' and not quality2 = 'evil' Then 'Gryffindor'
      When quality1 = 'studious' or quality2 = 'intelligent' Then 'Ravenclaw'
      When quality1 = 'hufflepuff' or quality2 = 'hufflepuff' Then 'Hufflepuff'
      else 'no suitable'
    End faculty
  From students
  )
  Select name, quality1, quality2, faculty
  From tmp
  Where not (faculty = 'no suitable')
);

--3
CREATE VIEW Gryffindor AS
SELECT name, quality1, quality2, total_points
FROM hogwarts_students
WHERE faculty = 'Gryffindor';

--4
CREATE MATERIALIZED VIEW sum_points_students AS
SELECT faculty, Sum(total_points)
FROM hogwarts_students
group by faculty;

--5
update ravenclaw
set total_points = 2
where name = 'Dr. Rod Conn' or name = 'Eladio Wehner';
SELECT * FROM ravenclaw

--6

 

PostgreSQL online editor

Write, Run & Share PostgreSQL queries online using OneCompiler's PostgreSQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for PostgreSQL. Getting started with the OneCompiler's PostgreSQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose database as 'PostgreSQL' and start writing queries to learn and test online without worrying about tedious process of installation.

About PostgreSQL

PostgreSQL is a open source relational database system and is also knows as Postgres.

Key Features:

  • Postgres is not only free and open-source but also it is highly extensible.
  • Custom Data types and funtions from various programming languaues can be introduced and the good part is compiling entire database is not required.
  • ACID(Atomicity, Consistency, Isolation, Durability) compliant.
  • First DBMS which implemented Multi-version concurrency control (MVCC) feature.
  • It's the default database server for MacOS.
  • It supports all major operating systems like Linux, Windows, OpenBSD,FreeBSD etc.

Syntax help

1. CREATE

CREATE command is used to create a table, schema or an index.

Syntax:

         CREATE TABLE table_name (
                column1 datatype,
                column2 datatype,
                ....);

2. ALTER

ALTER command is used to add, modify or delete columns or constraints from the database table.

Syntax

ALTER TABLE Table_name ADD column_name datatype;

3. TRUNCATE:

TRUNCATE command is used to delete the data present in the table but this will not delete the table.

Syntax

TRUNCATE table table_name;

4. DROP

DROP command is used to delete the table along with its data.

Syntax

DROP TABLE table_name;

5. RENAME

RENAME command is used to rename the table name.

Syntax

ALTER TABLE table_name1 RENAME to new_table_name1; 

6. INSERT

INSERT Statement is used to insert new records into the database table.

Syntax

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

7. SELECT

Select statement is used to select data from database tables.

Syntax:

SELECT column1, column2, ...
FROM table_name; 

8. UPDATE

UPDATE statement is used to modify the existing values of records present in the database table.

Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; 

9. DELETE

DELETE statement is used to delete the existing records present in the database table.

Syntax

DELETE FROM table_name where condition;