edureka sql

Data Definition Language (DDL) Deals with database schemas and descriptions, of how the data should reside in the database. 

Data Control Language (DCL) it includes commands such as GRANT and mostly concerned with rights, permissions and other controls of the database system

Data Manipulation Language (DML) Deals with data manipulation and includes most common SQL statements. Such as INSERT, UPDATE

Transaction Control Language (TCL) deals with transaction within a database

Types of Database: 

Popular Databases

What is a table? Table is collection of data in a tabular form. Rows and Columns 

Tuples and Attributes 

Tables Constraints: 

create table emp(

id int not null auto_increment,

ename varchar(20),

address varchar(30),

primary key(id)

);

drop table emp;

CREATE TABLE Student(

studentID INT NOT NULL AUTO_INCREMENT,

FName VARCHAR(20),

LName VARCHAR(20),

Address VARCHAR(30),

City VARCHAR(15),

Marks INT,

PRIMARY KEY(studentID)

);

select * from student; 

select fname, lname from student; 

SELECT FName

FROM Student

WHERE City='GOA';

SELECT * FROM Student 

WHERE FName = 'John' OR

Lname = 'Doe';

INSERT INTO Student(Fname, Lname, Address, City, Marks)

VALUES('Manooj','Sharma','#07, MG Road', 'Jaipur',438)

Aggregate Functions 

SELECT COUNT (studentID)

FROM Student; 

SELECT AVG(Marks)

FROM Student;

SELECT SUM(Marks)

FROM Student;

SELECT fname, lname, MIN(Marks)

FROM Student;

SELECT fname, lname, MIN(Marks)

FROM Student;

This query groups student by cities where they from 

SELECT COUNT(StudentID), City

FROM Student

GROUP BY City;

This query find students sum marks are larger than 500 marks 

SELECT Fname, SUM(Marks)

FROM student

GROUP BY fname

HAVING SUM(marks)>500;

Descending order is higher alpha to 

SELECT COUNT(studentID), city

FROM student 

GROUP BY city

ORDER BY city desc;

SELECT Fname, Lanme

FROM Student

WHERE Marks IS NOT NULL;

UPDATE Student

SET Fname = 'Amar', Lname = 'Kumar'

WHERE StudentID = 1;

DELETE FROM student

WHERE city = 'Mumbai';

SELECT StudentID, Fname, Lname

FROM Student

WHERE City IN ('Delhi', 'Goa', 'Pune', 'Bengaluru');

SELECT StudentID, Fname, Lname FROM Student

WHERE Marks BETWEEN 400 AND 500;

This will rename table column from "studentID" to just "id" 

SELECT studentID as id 

FROM student;

SELECT S.Fname, S.Lname

FROM Student as S;

SELECT dept_name, loc FROM dept WHERE loc LIKE 'c%';

SELECT dept_name, loc FROM dept WHERE loc LIKE 'chen_ _ _';

SELECT dept_name, loc FROM dept WHERE loc LIKE '%i';

SELECT dept_name, loc FROM dept WHERE loc LIKE 'c%i';

SELECT CASE(LOC)

WHEN 'Chennai' THEN 'TamilNadu'

WHEN 'Bangalore' THEN 'KR'

ELSE 'NO IDEA'

END

FROM DEPT

What is normalization?  

Processes of reducing the redundancy of data. 

Improves the Data Integrity 


This table had two telephone numbers 

What is TRIGGER? Triggers are` the SQL codes that are automatically executed in response to certain events on a particular table. These are used to maintain the integrity of the data.

CREATE TRIGGER sample_trigger

before INSERT

ON student

FOR EACH ROW

SET new.marks = new.marks+6;

DROP TRIGGER sample_trigger; 

SHOW TRIGGERS in edureka; 

CREATE TRIGGER calculate

before INSERT

ON student 

FOR EACH ROW

SET new.marks = new.marks+100;

What are Joins in SQL? 

JOINS in SQL are commands which are used to combine rows from two or more tables based on a related column between those tables. They are predominantly used when a user is trying to extract data from tables which have one-to-many or many-to-many relationships between them. 

INNER JOIN: This type of join returns those records which have matching values in both tables. So, if you perform an INNER JOIN operation between the Employee table and the Project table. all the tuples which have matching values in both the tables will be given as output. 

SELECT Employee.EmpID, Employee.EmpFname, Employee.EmpLname, Project.ProjectID, Project.ProjectName

FROM Employee

INNER JOIN Project ON Employee.EmpID=Project.EmpID;

LEFT JOIN: The LEFT JOIN or the LEFT OUTER JOIN returns all the records from the left table and also those records which satisfy a condition from the right table. Also, for the records having no matching values in the right table, the output or the result-set will contain the NULL values 

RIGHT JOIN: The RIGHT JOIN or the RIGHT OUTER JOIN returns all the records from the right table and also those records which satisfy a conditioons from the left table. Also, for the records having no matching values in the left table, the output or the result-set will contain the NULL values.

SELECT Employee.EmpID, Employee.EmpFname, Employee.EmpLname, Project.ProjectID, Project.ProjectName

FROM Employee

INNER JOIN Project ON Employee.EmpID=Project.EmpID;

FULL JOIN: FULL JOIN or the Full Outer Join returns all those records which either have a match in the left(Table1) or the right (Table2) table. 

SELECT Employee.EmpID, Employee.EmpFname, Employee.EmpLname, Project.ProjectID, Project.ProjectName

FROM Employee

LEFT JOIN Project ON Employee.EmpID=Project.EmpID;

UNION

SELECT Employee.EmpID, Employee.EmpFname, Employee.EmpLname, Project.ProjectID, Project.ProjectName

FROM Employee

RIGHT JOIN Project ON Employee.EmpID=Project.EmpID;

What is a Natural Join and which situations is a Natural Join used? 

How to map many-to-many relationships using joins? 

What is a Hash Join?

Hash Joins are aslo a type of joins which are used to join large tables or in an instance, where the user wants most of the joined table rows. The Hash Join algorithm is a two-step algorithm. 

What is Self and Cross Join?

Self Join: is a join of table to itself. This implies that each row in a table is joined with itself. 

The Cross Join is a type of join in which a join clause is applied to each row of a table to every row of the other table. Also, when the WHERE condition is used, this type of JOIN behaves as an INNER JOIN, and when the WHERE condition is not present, it behaves like a CARTESIAN product. 

SELECT DATEPART(mm, '12-OCT-2017') [10]

SELECT DATEADD(mm,2,'12/31/2015') [This will add two months so 2016-02-29]

SELECT DATEADD(dd,2,'12/31/2015') [This will add two days 

SELECT * FROM DEPT1

SELECT desptno AS DepartmentNO, Count(*) AS Count FROM DEPT1

GROUP BY deptno

SELECT * FROM EMPLOYEE

SELECT Job_id, COUNT(*) FROM employee

GROUP BY job_id

SELECT Job_id, Count(*) FROM employee

GROUP BY job_id

HAVING MIN(Salary) > 30000

USE empDB

GO

CREATE PROCEDURE dbo.uspGetDeptDetails

AS

SELECT * FROM dept

GO

Exec dbo.uspGetDeptDetails 

CREATE PROCEDURE dbo.uspGetDetail

@DeptNo nvarchar(30)

AS

SELECT * FROM

Dept

WHERE deptno = @DeptNo

GO

EXEC dbo.uspGetDetail @DeptNo = 30

@DeptNo nvarchar(30) = null

CREATE PROCEDURE dbo.uspGetDetailFrom

@DeptNo nvarchar(30) = NULL,

@DeptName nvarchar(60) = MULL

AS

SELECT * FROM

Dept

WHERE deptno = @DeptNo

AND dept_name = @DeptName

GO

EXEC dbo.uspGetDetailFrom @deptno = 50, @deptname = 'IT'

CREATE PROCEDURE dbo.uspGetCount

@DeptNo nvarchar(30)

AS

SELECT * FROM

Dept

WHERE deptno = @DeptNo

GO

EXEC dbo.uspGetDetail @DeptNo = 30