Git Hub
Git Hub
-----
DDL:Drop,Rename,Create,Alter,Truncate
DML:Update,Delete,Insert
TCL:Commit,RollBack,SavePoint
DCL:Grant,Revoke
DQL/DRL: Select
Q)Add column in table?
Q)Composite key ?
Q)unique and primary ?
A composite key is a combination of two or more columns in a table that can be used
to uniquely identify each row in the table when the columns are combined uniqueness
is guaranteed, but when it taken individually it does not guarantee uniqueness.
Q) Difference between truncate and Delete ?
Truncate Delete
------- -------
1.It is DDL. 1.It is DML command.
2.Perminant deletion. 2.Temparary deletion.
3.cannot delete specifice. 3.can delete specific.
Union: This operator returns all the values from the tables excluding
duplicate values
UnionAll: This operator returns all the values from the tables include
duplicate values
Intersection: This return common values from both tables.
Minus:the rows which are present in first table but not available in second
table.
Q) Constriant in SQL ?
Q) find the average salary of employees department wise along with other details?
select department_id,avg(sal) from employees group by department_id;
Select avg(sal) over (partition by department_id) as avg_dept, a.* from
employees a order by eployee_id;
Analytic Functions:
-------------------
To assign rank numbers to each row or else group of rows wise.
===================================================================================
=======================================================================
LAG: TO access previous subsequence row :
syntax:
LAG(next_col_name,[offset,defult_value) over ([partition by columnname]
order by columnname [asc/desc])
offset default value 1
default value for if subsequence row not exist
partition by for to do partiton the data
Ex: select ename,sal,lag(sal,1) over (order by sal desc) as prev_row
from emp;
LEAD: To access next subsequence row
LEAD(next_col_name,[offset,defult_value) over ([partition by columnname]
order by columnname [asc/desc])
offset default value 1
default value for if subsequence row not exist
Ex: select ename,sal,lead(sal,1) over (order by sal desc) as prev_row
from emp;
Q) display max salary with employee name and min salary and employee name ?
select min(ename) keep(dense_rank first order by sal) as min_ename,min(sal) as
min_sal,max(ename) keep(dense_rank last order by sal) as max_ename ,max(sal) as max
sal from emp;
Emp
ename esal dept
Selct dept ,sum(dept) From Emp e1 Join Emp e2 join e1.dept=e2.dept group by
e1.dept;
Select * from ( Select ename,sal,deptno,dense_rank() over(order by sal) as rnk
from Emp) where rnk=1;
JOINS:
-------
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table
EX: SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN
Orders ON Customers.CustomerID = Orders.CustomerID;
RIGHT (OUTER) JOIN: Returns all records from the right table, and the
matched records from the left table.
FULL (OUTER) JOIN: Returns all records when there is a match in either left
or right table.
Q)Identify the employees below avg sal and above avg sal of their dept (new col,
which says above or below)
SELECT * FROM employees WHERE salary > ALL(SELECT avg(salary)FROM employees
GROUP BY department_id);
3rd approach : CREATE TABLE TempTable SELECT DISTINCT id, day, month, year FROM
dates;
Q)Merge
MERGE TargetProducts AS Target
USING SourceProducts AS Source
ON Source.ProductID = Target.ProductID
-- For Inserts
WHEN NOT MATCHED BY Target THEN
INSERT (ProductID,ProductName, Price)
VALUES (Source.ProductID,Source.ProductName, Source.Price)
-- For Updates
WHEN MATCHED THEN UPDATE SET
Target.ProductName = Source.ProductName,
Target.Price = Source.Price
-- For Deletes
WHEN NOT MATCHED BY Source THEN
DELETE;
Q)Views
->it is database object
->it contains logical copy of data from table.
-> provide safety and increase db performance.
-> View maintain data dynamically.
Q)Datbase vs Schema ?