-- Имена дам create table Names (name char(10)); insert into Names values ('Winslow'), ('Marcolla'), ('Contee'), ('Natsiou'), ('Finch'); -- Позиции дам за столом create table Positions (pos int); insert into Positions values (1), (2), (3), (4), (5); -- Цвета платьев create table Colors (color char(10)); insert into Colors values ('red'), ('white'), ('purple'), ('blue'), ('green'); -- Родные города дам create table Cities (city char(10)); insert into Cities values ('Bailton'), ('Serkonos'), ('Freiport'), ('Morley'), ('Danuol'); -- Напитки дам create table Drinks (drink char(10)); insert into Drinks values ('absent'), ('coctail'), ('rum'), ('cider'), ('whiskey'); -- Ценности create table Items (item char(10)); insert into Items values ('ring'), ('diamond'), ('order'), ('cigar-case'), ('coulomb'); WITH s(pos, name, item, color, drink, city) AS ( SELECT pos, name, item, color, drink, city FROM Positions, Names, Colors, Drinks, Cities, Items WHERE ((name='Winslow' AND color = 'blue') OR (name!='Winslow' AND color != 'blue')) AND ((name='Marcolla' AND pos = 1) OR (name!='Marcolla' AND pos != 1)) AND ((color='white' AND pos = 2) OR (color!='white' AND pos != 2)) AND ((color='red' AND drink = 'whiskey') OR (color!='red' AND drink != 'whiskey')) AND ((city='Morley' AND color = 'green') OR (city!='Morley' AND color != 'green')) AND ((name='Finch' AND item = 'coulomb') OR (name!='Finch' AND item != 'coulomb')) AND ((city='Freiport' AND item = 'ring') OR (city!='Freiport' AND item != 'ring')) AND ((name='Contee' AND drink = 'absent') OR (name!='Contee' AND drink != 'absent')) AND ((city='Serkonos' AND drink = 'cider') OR (city!='Serkonos' AND drink != 'cider')) AND ((pos=3 AND drink = 'rum') OR (pos!=3 AND drink != 'rum')) AND ((name='Natsiou' AND city = 'Bailton') OR (name!='Natsiou' AND city != 'Bailton')) ) SELECT t1.name, t1.item, t2.name, t2.item, t3.name, t3.item, t4.name, t4.item, t5.name, t5.item FROM s t1 JOIN s t2 ON t2.name != t1.name AND t2.item != t1.item AND t2.color != t1.color AND t2.drink != t1.drink AND t2.city != t1.city JOIN s t3 ON t3.name NOT IN (t1.name, t2.name) AND t3.item NOT IN (t1.item, t2.item) AND t3.color NOT IN (t1.color, t2.color) AND t3.drink NOT IN (t1.drink, t2.drink) AND t3.city NOT IN (t1.city, t2.city) JOIN s t4 ON t4.name NOT IN (t1.name, t2.name, t3.name) AND t4.item NOT IN (t1.item, t2.item, t3.item) AND t4.color NOT IN (t1.color, t2.color, t3.color) AND t4.drink NOT IN (t1.drink, t2.drink, t3.drink) AND t4.city NOT IN (t1.city, t2.city, t3.city) JOIN s t5 ON t5.name NOT IN (t1.name, t2.name, t3.name, t4.name) AND t5.item NOT IN (t1.item, t2.item, t3.item, t4.item) AND t5.color NOT IN (t1.color, t2.color, t3.color, t4.color) AND t5.drink NOT IN (t1.drink, t2.drink, t3.drink, t4.drink) AND t5.city NOT IN (t1.city, t2.city, t3.city, t4.city) WHERE -- Мы хотим вывести дам в том порядке, в котором они сидели t1.pos = 1 AND t2.pos=2 AND t3.pos = 3 AND t4.pos=4 AND t5.pos=5 -- Дама в красном сидит левее дамы в пурпурном, но место 2 занято дамой в белом, поэтому остаются места 3 и 4 AND ( (t3.color='red' AND t4.color='purple' ) OR (t4.color='red' AND t5.color='purple') ) -- Рядом с дамой с портсигаром сидит дама из Морли AND ( (t1.item='cigar-case' AND t2.city='Morley') OR (t2.item='cigar-case' AND 'Morley' IN (t1.city, t3.city)) OR (t3.item='cigar-case' AND 'Morley' IN (t2.city, t4.city)) OR (t4.item='cigar-case' AND 'Morley' IN (t3.city, t5.city)) OR (t5.item='cigar-case' AND t4.city='Morley') ) -- Рядом с дамой с бриллиантом сидит дама из Дануолл AND ( (t1.item='diamond' AND t2.city='Danuol') OR (t2.item='diamond' AND 'Danuol' IN (t1.city, t3.city)) OR (t3.item='diamond' AND 'Danuol' IN (t2.city, t4.city)) OR (t4.item='diamond' AND 'Danuol' IN (t3.city, t5.city)) OR (t5.item='diamond' AND t4.city='Danuol') ) -- Рядом с дамой из Дануолла другая дама пила коктейль AND ( (t1.drink='coctail' AND t2.city='Danuol') OR (t2.drink='coctail' AND 'Danuol' IN (t1.city, t3.city)) OR (t3.drink='coctail' AND 'Danuol' IN (t2.city, t4.city)) OR (t4.drink='coctail' AND 'Danuol' IN (t3.city, t5.city)) OR (t5.drink='coctail' AND t4.city='Danuol') );
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.
PostgreSQL is a open source relational database system and is also knows as Postgres.
CREATE command is used to create a table, schema or an index.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
....);
ALTER command is used to add, modify or delete columns or constraints from the database table.
ALTER TABLE Table_name ADD column_name datatype;
TRUNCATE command is used to delete the data present in the table but this will not delete the table.
TRUNCATE table table_name;
DROP command is used to delete the table along with its data.
DROP TABLE table_name;
RENAME command is used to rename the table name.
ALTER TABLE table_name1 RENAME to new_table_name1;
INSERT Statement is used to insert new records into the database table.
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Select statement is used to select data from database tables.
SELECT column1, column2, ...
FROM table_name;
UPDATE statement is used to modify the existing values of records present in the database table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE statement is used to delete the existing records present in the database table.
DELETE FROM table_name where condition;