create table sailors(sid INT,sname VARCHAR(255),rating INT,age FLOAT); create table boats(bid int,bname VARCHAR(255),color VARCHAR(155)); create table reserves(sid int,bid int,day date); insert into sailors(sid,sname,rating,age) VALUES(22, 'Dustin', 7, 45.0), (29, 'Brutus', 1, 33.0), (31, 'Lubber', 8, 55.5), (32, 'Andy', 8, 25.5), (58, 'Rusty', 10, 35.0), (64, 'Horatio', 7, 35.0), (71, 'Zorba', 10, 16.0), (74, 'Horatio', 9, 35.0), (85, 'Art', 3, 25.5), (95, 'Bob', 3, 63.5); select * from sailors; insert into boats(bid, bname, color) values (101, 'Interlak', 'blue'), (102, 'Interlak', 'red'), (103, 'Clipper', 'green'), (104, 'Marine', 'red'); select * from boats; insert into reserves (sid,bid,day) values (22,101,'1998-10-10'), (22,102,'1998-10-10'), (22,103,'1998-08-10'), (22,104,'1998-07-10'), (31,102,'1998-10-11'), (31,103,'1998-06-11'), (31,104,'1998-12-11'), (64,101,'1998-05-09'), (64,102,'1998-08-09'), (74,103,'1998-08-09'); select * from reserves; --1)find the names and ages of all sailors -- select s.sname,s.age from sailors s; --2)find all sailors with a rating about 7 -- select * from sailors where rating > 7; -- -- --3)find the names of sailors who have Reserved boat 103 -- select s.sname from sailors s, reserves r where s.sid = r.sid and r.bid=103; -- select s.sname from sailors s where s.sid IN (select r.sid from reserves r where r.bid=103) -- select s.sname from sailors s where s.sid IN(select r.sid from reserves r where r.bid IN(select b.bid from boats b where b.color='red')); -- select s.sname from sailors s where EXISTS (select*from reserves r where r.bid=103 and r.sid=s.sid); -- select AVG (s.age) from sailors s where s.rating=10; -- select s.sname,s.age from sailors s where s.age = (SELECT MAX (s2.age) from sailors s2); -- select count(DISTINCT sname) from sailors; -- select s.sid from sailors s where s.rating>any(select s2.rating from sailors s2 where s2.sname='Horatio') -- select s.rating,min(s.age) from sailors s group by s.rating -- select s.rating,min(s.age) as minage from sailors s where s.age>=18 group by s.rating having count(*)>1 -- select b.bid,count(*) as reservationcount from boats b,reserves r where r.bid=b.bid group by b.bid having b.color='red';(Error) -- select s.rating,avg(s.age) as avgage from sailors s group by s.rating having 1<(select count(*)from sailors s2 where s.rating=s2.rating) -- select s.rating, AVG (s.age) as avgage from sailors S Where s.age>18 group by s.rating -- select s.sname from sailors s where s.sid =any (select r.sid from reserves r where r.bid=103); -- select s.sname from sailors s where s.sid >all (select r.sid from reserves r where r.bid=103); -- select s.sname from sailors s where s.sid >any (select r.sid from reserves r where r.bid=103); -- select s.sname from sailors s where s.sid <all (select r.sid from reserves r where r.bid=103); -- select s.sname from sailors s where s.sid <any (select r.sid from reserves r where r.bid=103); -- select s.sname from sailors s where s.sid = (select r.sid from reserves r where r.bid=103); -- select s.sname from sailors s where s.sid =all (select r.sid from reserves r where r.bid IN (select b.bid from boats b where b.color='red')); -- select s.sname from sailors s where s.sid =any (select r.sid from reserves r where r.bid IN (select b.bid from boats b where b.color='red')); -- select s.sname from sailors s where s.sid >all (select r.sid from reserves r where r.bid IN (select b.bid from boats b where b.color='red')); -- select s.sname from sailors s where s.sid <all (select r.sid from reserves r where r.bid IN (select b.bid from boats b where b.color='red')); -- select s.sname from sailors s where s.sid >ANY (select r.sid from reserves r where r.bid IN (select b.bid from boats b where b.color='red')); -- select s.sname from sailors s where s.sid <ANY (select r.sid from reserves r where r.bid IN (select b.bid from boats b where b.color='red'));
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;