CREATE TABLE EMP(
empID INTEGER PRIMARY KEY,
name TEXT,
dept TEXT,
address varchar(30),
salary INTEGER
);

insert into EMP values(23,'Ani','Design','Pune',7500000);
insert into EMP values(54,'Ashish','IT Support','Pune',100000000);
insert into EMP values(86,'Kartik','Analyst','Pune',499900);
insert into EMP values(44,'Rutuja','Software Eng.','Nagar',3333330);
insert into EMP values(73,'Buddhi','Developer','Satara',99999999);

SELECT* FROM EMP WHERE dept= 'Analyst';
SELECT*from EMP;

SELECT* FROM EMP WHERE name = 'Ani';
SELECT* FROM EMP;

update EMP set dept='EnTc' WHERE name='Ashish';
SELECT * FROM EMP;

 

CREATE TABLE DEPARTMENT (
  deptId INTEGER PRIMARY KEY,
  dname TEXT NOT NULL,
  dcompany TEXT NOT NULL,
  daddress varchar(20)
);

 

INSERT INTO DEPARTMENT VALUES (001, 'Sales', 'Capgemini', 'Hinjawadi');
INSERT INTO DEPARTMENT VALUES (002, 'Accounting', 'Cognizant', 'Hinjawadi');
INSERT INTO DEPARTMENT VALUES (003, 'Developer', 'IBM','Hinjawadi');
INSERT INTO DEPARTMENT VALUES (004, 'Testing', 'Infosys', 'Hinjawadi');
INSERT INTO DEPARTMENT VALUES (005, 'EnTc', 'Wipro', 'Hinjawadi');

 
SELECT * from DEPARTMENT;


delete from EMP WHERE dept='Software Eng';
SELECT * from EMP;
SELECT name, dept from EMP;
SELECT dept, name from EMP;

SELECT name as"Emp Name", dept from EMP;
SELECT name "Emp Name", salary,salary*0.1 as "Allowance", salary+(salary*0.1) as "Net Salary" from EMP;
SELECT* from EMP WHERE empID= 54 OR dept='EnTc'or name ='Ashish';

SELECT* from EMP WHERE dept <> 'Accounting';
SELECT* from DEPARTMENT WHERE deptId in (0001, 0004, 0003);
SELECT* from DEPARTMENT WHERE deptId between 0001 and 0004;


SELECT* from DEPARTMENT WHERE dname like 'A%';
SELECT* from DEPARTMENT order by dname;
SELECT* from EMP order by name;

INSERT INTO EMP (empID, name) values (32,'Priyanka');
SELECT* from EMP;
SELECT round(12.9);
SELECT trunc(12.43456,2);
SELECT empID,name, coalesce(dept,'Manager'), coalesce(salary,6000000) from EMP;

SELECT concat('Hello world');
SELECT concat('Hi ','How are you');
SELECT concat('Hi ','How are you') as "Concatenated String";
SELECT upper('Aarya');
SELECT lower('Aarya');

SELECT length('Welcome');
SELECT length(trim('Welcome'));
SELECT length('Welcome'), length(trim('Welcome   '));
SELECT length('Welcome   '),length(trim('Welcome   '));
SELECT length('  Welcome   '),length(ltrim(' Welcome  '));
SELECT length('  Welcome   '),length(rtrim(' Welcome  '));


SELECT substr('Welcome',4);
SELECT substr('Welcome',1,4);
SELECT strpos('Welcome','come');
SELECT strpos('Welcome','me');
SELECT strpos('Welcome','w');
SELECT strpos('Welcome', 'W');


SELECT upper(name) from EMP;
SELECT count(*) from EMP;
SELECT max(salary) from EMP;
SELECT min(salary) from EMP;
SELECT avg(salary) from EMP;
SELECT trunc(salary) from EMP;

SELECT trunc(avg(salary),2) from EMP;
update EMP set dept ='Manager',salary= 5600000 where empID= 32;
SELECT* from EMP;
SELECT dept, sum(salary) from EMP group by dept;
SELECT dept, sum(salary) from EMP group by dept order by dept;


SELECT dept, sum(salary) from EMP group by dept having sum(salary) >499900 order by sum desc;



 













 

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;