PostgreSQL Exercise 3

select dname,count(ename),avg(sal),sum(sal)
from emp e,dept d
where e.deptno=d.deptno
group by dname;

select dname,count(ename),avg(sal),sum(sal)
from emp e inner join dept d
on e.deptno=d.deptno
group by dname;

psql -h 192.168.0.100 -U testuser
create database testdb
\c testdb

SELECT *
FROM sdb."Student" as a
where a.Sid in ('01','02')
intersect
SELECT *
FROM sdb."Student" as b
where b.Sid in ('01')

SELECT *
FROM sdb."Student" as a
where a.Sid in ('01','02')
intersect all
SELECT *
FROM sdb."Student" as b
where b.Sid in ('01')

SELECT *
FROM sdb."Student" as a
where a.Sid in ('01','02')
union all
SELECT *
FROM sdb."Student" as b
where b.Sid in ('01')

SELECT *
FROM sdb."Student" as a
where a.Sid in ('01','02')
union
SELECT *
FROM sdb."Student" as b
where b.Sid in ('01')

SELECT *
FROM sdb."Student" as a
where a.Sid in ('01','02')
union all
SELECT *
FROM sdb."Student" as b
where b.Sid in ('01')

SELECT *
FROM sdb."Student" as a
where a.Sid in ('01','02')
except
SELECT *
FROM sdb."Student" as b
where b.Sid in ('01')

SELECT *
FROM sdb."Student" as a
where a.Sid in ('01','02','03')
except all
SELECT *
FROM sdb."Student" as b
where b.Sid in ('01')

select s.Sid
FROM sdb."Student" as s
left join sdb.sc
on s.Sid=sc.Sid
group by s.Sid
having count(sc.Cid)<(select count(Cid) from sdb.course);

select abs(-11);
select mod(123,6);
select ceil(1.1);
select floor(1.9);
select round(123.123,0);
select trunc(123.123,3);
select ascii('A');
select chr(66);
select lower('SQL');
select upper('asd');
select initcap('sda');
select substr('asdfgh',2,3);
select length('SQL');
select position('r'in 'string');
select lpad('h',10,'*');
select rpad('g',10,'*');
select trim('s'from 'string');
select replace('ASD','SD','sd');

CURRENT_DATE SELECT;
SELECT CURRENT_TIME;
SELECT Extract (from Day timestamp '2019-10-17 14:05:40'); - extracting from the date of the timeline subdomain
select date_trunc ( 'month', now ());

select current_time+time'00:00:01';
select current_time+time'00:01';
select current_time+time'01:00';
select current_time+interval'1 day';
select current_time+interval'1 week';
select current_time+interval'1 month';


select to_char(current_timestamp,'HH12:MI:SS');
select to_char(interval'15h 2m 12s','HH24:MI:SS');
select to_char(125,'999');
select to_char(125.8::real,'99909');
select to_date('05 Dec 2000','DD MON YYYY');
select to_number('12.454.8-','99G999D99S');
select to_timestamp('05 Dec 2000','DD MON YYYY');

select current_database (); - the current database name
select current_schema (); - the name of the current mode
select current_user; - user name in the current environment
select version (); - the version information

 

Guess you like

Origin www.cnblogs.com/hole/p/11699712.html