Sql Basis Flashcards
Sql NOT
Select * from employee where NOT gender=‘female’
Sql BETWEEN
Select * from employee where age between 20 and 25
Sql COUNT
Select count(*) from employee
5 sql string function
ltrim(), lower(),upper(), reverse(), substring(string,start,lenght)
Sql AVG
Select avg(age) from employee
Average age, group by dept
Select avg(age),dpmt from employee group by dpmt order by avg(age) desc
Group by dpmt, avg(salary) when avg salary>100k
Select avg(salary),dptm from employee group by dpmt having avg(salary)>100000 order by avg(salary) desc.
GROUP BY - HAVING - ORDER
Having must be used in conjonction of group by
Sql TRUNCATE
Truncate table employee;
- delete all records
Sql INNER JOIN
Records only matching in the 2 tables
Select employee.name, employee.dpmt,department.name,department.location from employee inner join department on employee.dpmt=department.name
Sql FULL JOIN
All records from both tables
Sql LEFT JOIN
All records from first table and matching records from table 2
Delete command with join
Delete employee where dept. Loc=NY
Delete employee from employee join department on employee.dept=department.name where department.location=‘NY’
Sql UNION
Records from 2 tables without duplicate
Select… union select…
UNION ALL : with duplicate
Sql Operator merging 2 select
Union : no duplicate
Union all: with duplicate
Except: remove record from second table
Intersect: common columns from the 2 tables
Create VIEW
Create VIEW myView as
Select x from y where z
Drop a view
Drop view myview
Alter, add column
Alter table employee
Add dob date;
Alter table, drop column
Alter table employee
Drop column dob;