SQL & NOSQL QUERIES

SQL QUERIES

query to fetch unique values
SELECT DISTINCT MAJOR from STUDENT; 
or
SELECT MAJOR FROM STUDENT GROUP BY(MAJOR);

SELECT Major, COUNT(*) as TOTAL_COUNT FROM Student WHERE MAJOR = 'Computer Science';

SELECT MAJOR, COUNT(MAJOR) from Student group by MAJOR order by COUNT(MAJOR) DESC;

SELECT * from Student ORDER BY GPA DESC LIMIT 5;

Print the first 3 characters of FIRST_NAME from Student table:
SELECT SUBSTRING(FIRST_NAME, 1, 3)  FROM Student;

SQL query to find the position of alphabet (‘a’) int the first name column ‘Shivansh’ from Student table:
SELECT INSTR(FIRST_NAME, 'a') FROM Student WHERE FIRST_NAME = 'Shivansh';

SQL query that fetches the unique values of MAJOR Subjects from Student table and print its length:
SELECT MAJOR,LENGTH(MAJOR) FROM Student GROUP BY(MAJOR;         
or                                                                                          
SELECT DISTINCT MAJOR, LENGTH(MAJOR) FROM Student;  

SQL query to print all Student details from Student table order by FIRST_NAME Ascending and MAJOR Subject descending:
SELECT * FROM Student ORDER BY FIRST_NAME , MAJOR DESC;

SQL query to print details of the Students whose FIRST_NAME ends with ‘a’:
SELECT * FROM Student WHERE FIRST_NAME LIKE '%a';

SQL query to print details of the Students whose FIRST_NAME ends with ‘a’ and contains six alphabets.
SELECT * FROM Student WHERE FIRST_NAME LIKE '_____a';

SQL query to print details of the Students whose GPA lies between 9.00 and 9.99.
SELECT * FROM Student WHERE GPA BETWEEN 9.00 AND 9.99;

SQL query to fetch the count of Students having Major Subject ‘Computer Science’.
SELECT Major, COUNT(*) as TOTAL_COUNT FROM Student WHERE MAJOR = 'Computer Science';

SQL query to fetch the no. of Students for each MAJOR subject in the descending order.
SELECT MAJOR, COUNT(MAJOR) from Student group by MAJOR order by COUNT(MAJOR) DESC;

SQL query to show only odd rows from Student table.
SELECT * FROM Student WHERE student_id % 2 != 0;

SQL query to determine the nth (say n=5) highest GPA from a table.
SELECT * FROM Student ORDER BY GPA DESC LIMIT 5, 1;

SQL query to determine the 5th highest GPA without using LIMIT keyword.
SELECT * FROM Student s1 
WHERE 4 = (
    SELECT COUNT(DISTINCT (s2.GPA)) 
    FROM Student s2
    WHERE s2.GPA >= s1.GPA
);

SQL query to show the second highest GPA from a Student table using sub-query.
SELECT MAX(GPA) FROM Student WHERE GPA NOT IN(SELECT MAX(GPA) FROM Student);

SQL query to fetch the MAJOR subject that have less than 4 people in it.
SELECT MAJOR, COUNT(MAJOR) AS MAJOR_COUNT FROM Student GROUP BY MAJOR HAVING COUNT(MAJOR) < 4;

SQL query to show all MAJOR subject along with the number of people in there.
SELECT MAJOR, COUNT(MAJOR) AS ALL_MAJOR FROM Student GROUP BY MAJOR;

SQL query to show the last record from a table.
SELECT * FROM Student WHERE STUDENT_ID = (SELECT MAX(STUDENT_ID) FROM STUDENT);

SQL query to fetch the first row of a table.
SELECT * FROM Student WHERE STUDENT_ID = (SELECT MIN(STUDENT_ID) FROM Student);

SQL query to fetch the last five records from a table.
SELECT * 
FROM (
    SELECT * 
    FROM Student 
    ORDER BY STUDENT_ID DESC 
    LIMIT 5
) AS subquery
ORDER BY STUDENT_ID;

SQL query to show the current date and time.
Query to get current date : 
  SELECT CURDATE();
Query to get current date and time : 
  SELECT NOW();

SQL query to show the top 3 students with the highest GPA:
  SELECT * FROM Student ORDER BY GPA DESC LIMIT 3;


SQL Indexes
An index is a schema object. It is used by the server to speed up the retrieval of rows by using a pointer.
Creating an Index
  Syntax
    CREATE UNIQUE INDEX index
    ON TABLE (column1, column2,…..);

SQL Views
Views in SQL are a kind of virtual table.
A view also has rows and columns like tables, but a view doesn’t store data on the disk like a table. 
View defines a customized query that retrieves data from one or more tables, and represents the data as if it was coming from a single source.
  Syntax
    CREATE VIEW view_name AS
      SELECT column1, column2.....
      FROM table_name
      WHERE condition;
Rules:
should be created from a single table.
should not include GROUP BY clause or ORDER BY clause.
should not have the DISTINCT keyword.
should not be created using nested queries or complex queries.


NOSQL QUERIES

Example selects from the inventory collection all documents where the status equals "D":
db.collection('inventory').find({ status: 'D' });

Example retrieves all documents from the inventory collection where status equals either "A" or "D":
db.collection('inventory').find({
  status: { $in: ['A', 'D'] }
});

Specify AND Conditions:
Example retrieves all documents in the inventory collection where the status equals "A" and qty is less than ($lt) 30:
db.collection('inventory').find({
  status: 'A',
  qty: { $lt: 30 }
});

Specify OR Conditions:
db.collection('inventory').find({
  $or: [{ status: 'A' }, { qty: { $lt: 30 } }]
});

Specify AND as well as OR Conditions:
db.collection('inventory').find({
  status: 'A',
  $or: [{ qty: { $lt: 30 } }, { item: { $regex: '^p' } }]
});

Update a Single Document
update the first document where item equals "paper":
await db.collection('inventory').updateOne(
  { item: 'paper' },
  {
    $set: { 'size.uom': 'cm', status: 'P' },
    $currentDate: { lastModified: true }
  }
);

Update Multiple Documents
update all documents where qty is less than 50:
await db.collection('inventory').updateMany(
  { qty: { $lt: 50 } },
  {
    $set: { 'size.uom': 'in', status: 'P' },
    $currentDate: { lastModified: true }
  }
);

await db.collection('inventory').deleteMany({});
await db.collection('inventory').deleteMany({ status: 'A' });
await db.collection('inventory').deleteOne({ status: 'D' });

Comments

Popular posts from this blog

PL/SQL

JAVA8 Features

Build Automation