0% found this document useful (0 votes)
28 views5 pages

SQL Queries and Concepts

Uploaded by

Pranav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

SQL Queries and Concepts

Uploaded by

Pranav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL QUERIES AND CONCEPTS

1. Select Statement (select data from a database)


- Select * from table_name;
- SELECT column1, column2 FROM table_name;

2. Select Distinct is used to return only distinct values.


- SELECT DISTINCT column1, column2 FROM table_name;

3. WHERE: It is used to extract only those records that fulfill a specified condition.
- SEELCT ‘column’ FROM ‘table’ WHERE ‘condition’;
- SELECT * FROM Customers WHERE Country='Mexico';
 SELECT * FROM PRODUCTS WHERE PRICE = 18;
 SELECT * FROM PRODUCTS WHERE PRICE >= 30;
 SELECT * FROM PRODUCTS WHERE PRICE <= 30;
 SELECT * FROM Products WHERE Price <> 18; || SELECT * FROM Products WHERE Price != 18;
 SELECT * FROM Products WHERE Price BETWEEN 50 AND 60;
 SELECT * FROM Customers WHERE City LIKE 's%';
 SELECT * FROM Customers WHERE City IN ('Paris' , 'London');

4. ORDER BY: Sorts in Ascending or Descending order


- SELECT * FROM Customers ORDER BY Country;
- SELECT * FROM Customers ORDER BY Country, CustomerName; (Basically if 2 countries are same, it orders by CustomerName)

5. AND: SELECT * FROM Customers WHERE Country = 'Spain' AND CustomerName LIKE 'G%';

6. OR: SELECT * FROM Customers WHERE City = 'Berlin' OR CustomerName LIKE 'G%' OR Country = 'Norway';

7. NOT OPERATOR: Gives opposite result. Kind of a negative result


- SELECT * FROM Customers WHERE NOT Country = 'Spain' ;
- NOT LIKE: SELECT * FROM Customers WHERE CustomerName NOT LIKE 'A%';
- NOT IN: SELECT * FROM Customers WHERE CITY NOT IN (‘PARIS’,’TOKYO’);
- NOT BETWEEN: SELECT * FROM Customers WHERE PRICE NOT BETWEEN 20 AND 50;

8. IS NULL AND IS NOT NULL:


SELECT * FROM CUSTOMERS WHERE NAME IS NULL;
SELECT * FROM CUSTOMERS WHERE NAME IS NOT NULL;

9. UPDATE: Used to modify existing records in a table


- UPDATE TABLE_NAME SET COL1=VAL1 WHERE Condition
- UPDATE Customers SET ContactName = 'Alfred Schmidt', City= 'Frankfurt' WHERE CustomerID = 1;
- UPDATE Customers SET ContactName = 'Juan' (Sets all ContactName to Juan)

10. DELETE: Used to delete a record form a database


- DELETE FROM CUSTOMERS WHERE CONDITION
- DELETE FROM CUSTOMERS (DELETES ALL ROWS BUT KEEPS THE ATTRIBUTES INTACT)

11. MIN() & MAX() Functions


- SELECT MIN(COL_NAME) FROM TABLE WHERE CONDITION
- SELECT MAX(COL_NAME) FROM TABLE WHERE CONDITION

12. COUNT : Returns the count of the number of rows matching the specified criterion
- SELECT COUNT(*) FROM PRODUCTS (Returns the total number of rows)
- SELECT COUNT(COLUMN_NAME) FROM TABLE WHERE CONDITION
- SELECT COUNT(DISTINCT COLUMN_NAME) FROM TABLE WHERE CONDITION

13. SUM() : Returns the sum of a numeric column


- SELECT SUM(COL_NAME) FROM TABLE_NAME WHERE CONDITION
- SELECT SUM(Quantity) FROM OrderDetails WHERE ProdictId = 11;

14. AVG: Returns avg of numeric column


- SELECT AVG(COL_NAME) FROM TABLE_NAME WHERE CONDITION

15. SELECT* FROM TABLE_NAME WHERE COL_NAME LIKE ‘anything from the image given below’

16. IN SYNTAX:
- SELECT * FROM Customers WHERE Country IN ('Germany', 'France', 'UK');
- SELECT * FROM Customers WHERE Country NOT IN ('Germany', 'France', 'UK');
- SELECT * FROM Customers WHERE Country IN (SELECT Country FROM Suppliers);

17. BETWEEN: Selects values within a given range


- SELECT COLUMN_NAME FROM TABLE_NAME WHERE COLUMN_NAME BETWEEN VAL1 & VAL2;
- SELECT * FROM Products WHERE ProductName BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni' ORDER BY
ProductName;

JOINS IN SQL
1. INNER JOIN: Selects rows from both the tables which overlap based on a common column.
- SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE FROM Student INNER JOIN StudentCourse ON
Student.ROLL_NO = StudentCourse.ROLL_NO;

+
2. LEFT JOIN:
SELECT Student.NAME,StudentCourse.COURSE_ID FROM Student LEFT JOIN StudentCourse ON StudentCourse.ROLL_NO =
Student.ROLL_NO;

3. Right Join: Similar to left join


SELECT Student.NAME,StudentCourse.COURSE_ID FROM Student RIGHT JOIN StudentCourse ON StudentCourse.ROLL_NO =
Student.ROLL_NO;
4. FULL JOIN: SELECT Student.NAME,StudentCourse.COURSE_ID FROM Student FULL JOIN StudentCourse ON
StudentCourse.ROLL_NO = Student.ROLL_NO;

18. UNION: The UNION operator is used to combine the result-set of two or more SELECT statements.
 Every SELECT statement within UNION must have the same number of columns.
 The columns must also have similar data types.
 The columns in every SELECT statement must also be in the same order.

- SELECT City FROM Customers UNION SELECT City FROM Suppliers.


- SELECT City FROM Customers UNION ALL SELECT City FROM Suppliers. (UNION ALL allows duplicate values)
- SELECT City, Country FROM Customers WHERE Country='Germany' UNION SELECT City, Country FROM
Suppliers WHERE Country='Germany'
- SELECT City, Country FROM Customers WHERE Country='Germany' UNION ALL SELECT City, Country FROM
Suppliers WHERE Country='Germany'

19) Group By: Groups rows having same values to summary rows. Generally used with Sum(), Count(), Max(), Min()
- SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
- SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country ORDER BY COUNT(CustomerID) DESC;

20) HAVING(): Introduced because WHERE() cannot be used with aggregation functions like Count(), Max(), Min()
- SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5;
-
IMPORTANT QUESTION:
1) How to print duplicate rows in a table?
A) Select col_name from table_name group by col_name having count(col_name)>1;
Eg: select name,emp_id,salary,department,age from employees group by name,emp_id,salary,department,age having
count(*)>1;

2) How to print the nth largest salary in a table


A) select name, salary from employees order by salary desc offset n-1 rows fetch next 1 row only;

3) KEYS IN DBMS:
They are one or more attributes in table. Used for uniquely identifying tuple in a table.

a) Super key: It is the same as


a) Candidate key:

You might also like