Search the Blog

Thursday, October 15, 2020

Top 1000 SQL Query

 

SQL SELECT Statement

SELECT column1, column2, ... FROM table_name

SELECT Student_Id, Student_Name FROM Students;

 

 

SELECT * FROM table_name

 SELECT * FROM Students;

 

SQL SELECT DISTINCT Statement

SELECT DISTINCT column1, column2, ... FROM table_name;

 SELECT Student FROM Students;

SELECT DISTINCT  Student FROM Students;

 

 

SQL WHERE Clause

SELECT column1, column2, ...FROM table_name WHERE condition

Operator-Equal

 SELECT * FROM Students WHERE Student_Name='Sajan';

Operator-Greater than

 SELECT * FROM Students WHERE Salary > 1000

Operator-Less than

 SELECT * FROM Students WHERE Salary < 1000

Operator-Greater than or equal

 SELECT * FROM Students WHERE Salary >= 1000

Operator- Less than or equal

 SELECT * FROM Students WHERE Salary <= 1000

Operator-  Not equal

 SELECT * FROM Students WHERE Salary <> 1000

 SELECT * FROM Students WHERE Salary != 1000

Operator-  BETWEEN

 SELECT * FROM Students WHERE Salary  BETWEEN 1000 AND 2000

Operator-  LIKE

 SELECT * FROM Students WHERE  Student_Name Like '% SQL%'

Operator-  IN

 SELECT * FROM Students WHERE  Stalary IN (1000,2000,3000)



SQL AND

 SELECT column1, column2, ... 

FROM table_name 

WHERE condition1 AND condition2 AND condition3 ...;
 

Select * from Student where Salary =1000 AND Student_Name Like '% SQL%'

 

SQL OR

 SELECT column1, column2, ... 

FROM table_name 

WHERE condition1 OR condition2 OR condition3 ...;
 

Select * from Student where Salary =1000 OR Student_Name Like '% SQL%'

 

SQL NOT

 SELECT column1, column2, ... 

FROM table_name 

WHERE condition1 OR condition2 ! condition3 ...;
 

Select * from Student where Salary =1000 ! Student_Name Like '% SQL%'

 

SQL ORDER 

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
 

 Select * from Student Order by Student_name

SQL INSERT INTO Statement

With column name

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);  


INSERT INTO Student(Student_name,Salary)
VALUES ('Sajan', 10000); 

 

 Without Column Name

INSERT INTO table_name
VALUES (value1, value2, value3, ...);
 

 INSERT INTO Student VALUES ('Sajan', 10000);   

 

SQL NULL Values

IS NULL Syntax

SELECT column_names
FROM table_name
WHERE column_name IS NULL;  

SELECT Student_Name
FROM Student
WHERE Salary IS NULL;  

 

IS NOT NULL Syntax

SELECT Student_Name
FROM Student
WHERE Salary IS NOT NULL;  



SQL UPDATE Statement

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition

Updating the Single Row

UPDATE Student
SET Student_Name = 'Sawan', Salary= 1000000
WHERE StudentID = 1;
 

 

Updating the All Row

UPDATE Student
SET Salary= 1000000
  

SQL DELETE Statement

Delete Single row of data

DELETE FROM table_name WHERE condition;

DELETE FROM Student WHERE StudentName = 'Sawan'; 

 

Delete all records

DELETE FROM Student;

 

SQL TOP Clause

SELECT TOP number | percent column_name(s)
FROM table_name
WHERE condition;

 

SELECT TOP 10 Salary
FROM Student_name
WHERE where salary <100000;
 

 

SELECT TOP 50 percent Salary
FROM Student_name
WHERE where salary <100000;
  

 

SQL MIN() Functions

SELECT MIN(column_name)
FROM table_name
WHERE condition

SELECT MIN(Salary)
FROM

Student_name WHERE where salary <100000;  

 

SELECT MIN(Salary)
FROM

Student_name ;  

SQL  MAX() Functions

SELECT MAX(column_name)
FROM table_name
WHERE condition;  

SELECT MAX(Salary)
FROM

Student_name WHERE where salary >100000 

 

SELECT MAX(Salary)
FROM

Student_name ;    

 

The SQL COUNT() Functions

 SELECT COUNT(column_name)
FROM table_name
WHERE condition;

SELECT COUNT(StudentID)
FROM
Student;

The SQL AVG() Functions

 SELECT AVG(column_name)
FROM table_name
WHERE condition

SELECT AVG(Salary)
FROM
Student;

The SQL  SUM() Functions

SELECT SUM(column_name)
FROM table_name
WHERE condition;

 SELECT SUM(Salary)
FROM
Student;

 

SQL LIKE Operator

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern

 

SELECT * FROM Student
WHERE StudentName LIKE 'a%';

 

SQL IN Operator

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);  

SELECT StudentName
FROM

Student WHERE Salary IN (1000,2000,3000);  

 

SELECT StudentName
FROM

Student WHERE Studentid IN (Select Studentid from Student Where Salary>2000);  

 

Translate