├── AS.sql ├── CASE.sql ├── CAST.sql ├── CRUD.sql ├── DISTINCT.sql ├── Excercises.sql ├── FUNCTIONS.sql ├── Full Outer Join.sql ├── GROUPBY.sql ├── HAVING.sql ├── Hackerrank SQL solutions ├── African Cities.sql ├── Average population of each continent.sql ├── Customer Spending.sql ├── Invoices per country.sql ├── Japanese Cities' Names.sql ├── New Companies.sql ├── Ollivander's Inventory.sql ├── Placements.sql ├── Revising the Select Query I.sql ├── Revising the Select Query II.sql ├── Select All.sql ├── Select By ID.sql ├── Weather Observation Station 1.sql ├── Weather Observation Station 3.sql ├── japancities.sql └── population census.sql ├── INNOTIN.sql ├── Inner Join.sql ├── LIMIT.sql ├── Left Outer Join.sql ├── Math Operations.sql ├── ORDERBY.sql ├── README.md ├── Right Outer Join.sql ├── SHOW_SELECT.sql ├── SQL Practical ├── MongoDB dumpyard │ ├── mongodb1practical.txt │ ├── mongodb2practical.txt │ ├── mongodb3practical.txt │ ├── mongodb4practical.txt │ ├── mongodb5practical.txt │ └── mongodb6practical.txt ├── MySQL dumpyard │ ├── mysql1.txt │ ├── mysql2.txt │ ├── mysql3and4.txt │ ├── mysql5.txt │ ├── mysql6.txt │ ├── mysql7.txt │ └── mysql8.txt └── PLSQL dumpyard │ ├── plsql1.txt │ ├── plsql2.txt │ ├── plsql3.txt │ ├── plsql4.txt │ ├── plsql5.txt │ ├── plsql6.txt │ └── plsql7.txt ├── Self Join.sql ├── String Operations.sql ├── Subquery.sql ├── UPDATE_DELETE.sql ├── WHERE.sql ├── WILDCARDS.sql ├── custom_columns.sql ├── table_creation.sql └── to_char & Extract.sql /AS.sql: -------------------------------------------------------------------------------- 1 | SELECT customer_id, SUM(amount) AS total_spend 2 | FROM payment 3 | GROUP BY customer_id 4 | HAVING SUM(amount) > 100; 5 | -------------------------------------------------------------------------------- /CASE.sql: -------------------------------------------------------------------------------- 1 | SELECT customer_id, 2 | CASE 3 | WHEN (customer_id <= 100) THEN 'Premium' 4 | WHEN (customer_id BETWEEN 100 AND 200) THEN 'Plus' 5 | ELSE 'Regular' 6 | END AS customer_class 7 | FROM customer 8 | 9 | SELECT customer_id, 10 | CASE customer_id 11 | WHEN 2 THEN 'Winner' 12 | WHEN 5 THEN 'Second Place' 13 | ELSE 'Normal' 14 | END AS raffle_results 15 | FROM customer 16 | 17 | SELECT 18 | SUM(CASE rental_rate 19 | WHEN 0.99 THEN 1 20 | ELSE 0 21 | END) AS bargains, 22 | SUM( 23 | CASE rental_rate 24 | WHEN 2.99 THEN 1 25 | ELSE 0 26 | END) AS regular, 27 | SUM( 28 | CASE rental_rate 29 | WHEN 4.99 THEN 1 30 | ELSE 0 31 | END) AS premium 32 | FROM film*/ 33 | 34 | 35 | SELECT 36 | SUM( 37 | CASE rating 38 | WHEN 'R' THEN 1 39 | ELSE 0 40 | END) AS r, 41 | SUM( 42 | CASE rating 43 | WHEN 'PG' THEN 1 44 | ELSE 0 45 | END) AS pg, 46 | SUM( 47 | CASE rating 48 | WHEN 'PG-13' THEN 1 49 | ELSE 0 50 | END) AS pg13 51 | FROM film; 52 | 53 | 54 | 55 | CREATE VIEW customer_info AS 56 | SELECT first_name, last_name, address FROM customer 57 | INNER JOIN address 58 | ON customer.address_id = address.address_id; 59 | 60 | SELECT * FROM customer_info; 61 | 62 | 63 | 64 | SELECT ( 65 | SUM(CASE 66 | WHEN department = 'A' THEN 1 67 | ELSE 0 68 | END 69 | ) / SUM( 70 | CASE 71 | WHEN department = 'B' THEN 1 72 | ELSE 0 73 | END 74 | ) 75 | ) AS ratio FROM depts; 76 | 77 | 78 | SELECT ( 79 | SUM(CASE 80 | WHEN department = 'A' THEN 1 81 | ELSE 0 82 | END 83 | ) / NULLIF(SUM( 84 | CASE 85 | WHEN department = 'C' THEN 1 86 | ELSE 0 87 | END), 0 88 | ) 89 | ) AS ratio FROM depts; 90 | 91 | -------------------------------------------------------------------------------- /CAST.sql: -------------------------------------------------------------------------------- 1 | SELECT CAST ('5' AS INTEGER); 2 | 3 | SELECT CHAR_LENGTH(CAST(inventory_id AS VARCHAR)) FROM rental; 4 | -------------------------------------------------------------------------------- /CRUD.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE account( 2 | user_id SERIAL PRIMARY KEY, 3 | username VARCHAR(50) UNIQUE NOT NULL, 4 | password VARCHAR(50) NOT NULL, 5 | email VARCHAR(250) UNIQUE NOT NULL, 6 | created_on TIMESTAMP NOT NULL, 7 | last_login TIMESTAMP 8 | ) 9 | 10 | CREATE TABLE job( 11 | job_id SERIAL PRIMARY KEY, 12 | job_name VARCHAR(200) UNIQUE NOT NULL 13 | ) 14 | 15 | CREATE TABLE account_job( 16 | user_id INTEGER REFERENCES account(user_id), 17 | job_id INTEGER REFERENCES job(job_id), 18 | hire_date TIMESTAMP 19 | ) 20 | 21 | 22 | 23 | INSERT INTO account (username, password, email, created_on) 24 | VALUES 25 | ('Jose', 'password', 'jose@mail.com', CURRENT_TIMESTAMP) 26 | 27 | 28 | 29 | INSERT INTO job (job_name) 30 | VALUES 31 | ('Astronaut floating in space') 32 | 33 | INSERT INTO job (job_name) 34 | VALUES 35 | ('President') 36 | 37 | INSERT INTO account_job (user_id, job_id, hire_date) 38 | VALUES 39 | (1, 1, CURRENT_TIMESTAMP) 40 | 41 | 42 | UPDATE account 43 | SET last_login = CURRENT_TIMESTAMP; 44 | 45 | UPDATE account 46 | SET last_login = created_on; 47 | 48 | UPDATE account_job 49 | SET hire_date = account.created_on 50 | FROM account 51 | WHERE account_job.user_id = account.user_id; 52 | 53 | UPDATE account 54 | SET last_login = CURRENT_TIMESTAMP 55 | RETURNING email, last_login, created_on; 56 | 57 | INSERT INTO job (job_name) 58 | VALUES 59 | ('Cowboy'); 60 | 61 | DELETE FROM job 62 | WHERE job_id = 2; 63 | 64 | 65 | ALTER TABLE information 66 | RENAME TO new_information; 67 | 68 | ALTER TABLE new_information 69 | RENAME COLUMN person TO people; 70 | 71 | ALTER TABLE new_information 72 | ALTER COLUMN people DROP NOT NULL; 73 | 74 | ALTER TABLE new_information 75 | DROP COLUMN people; 76 | 77 | CREATE TABLE employees( 78 | emp_id SERIAL PRIMARY KEY, 79 | first_name VARCHAR (50) NOT NULL, 80 | last_name VARCHAR (50) NOT NULL, 81 | birthdate DATE CHECK (birthdate > '1900-01-01'), 82 | hire_date DATE CHECK (hire_date > birthdate), 83 | salary INTEGER CHECK (salary > 0) 84 | ) 85 | 86 | INSERT INTO employees ( 87 | first_name, 88 | last_name, 89 | birthdate, 90 | hire_date, 91 | salary 92 | )VALUES 93 | ('Sammy', 94 | 'Smith', 95 | '1899-11-03', 96 | '2010-01-01', 97 | 100) 98 | 99 | 100 | CREATE TABLE teachers( 101 | teacher_id SERIAL PRIMARY KEY, 102 | first_name VARCHAR (100), 103 | last_name VARCHAR (100), 104 | homeroom_number INTEGER, 105 | department VARCHAR (100), 106 | email VARCHAR (200) UNIQUE, 107 | phone INTEGER UNIQUE 108 | ); 109 | 110 | CREATE TABLE students( 111 | student_id SERIAL PRIMARY KEY, 112 | first_name VARCHAR(100), 113 | last_name VARCHAR(100), 114 | homeroom_number INTEGER, 115 | phone INTEGER UNIQUE NOT NULL, 116 | email VARCHAR (200) UNIQUE, 117 | graduation_year INTEGER 118 | ); 119 | 120 | SELECT * FROM students; 121 | 122 | INSERT INTO students ( 123 | first_name, 124 | last_name, 125 | homeroom_number, 126 | phone, 127 | graduation_year 128 | )VALUES( 129 | 'Mark', 130 | 'Watney', 131 | 5, 132 | 777-555-1234, 133 | 2035 134 | ); 135 | 136 | SELECT * FROM teachers; 137 | 138 | INSERT INTO teachers( 139 | first_name, 140 | last_name, 141 | homeroom_number, 142 | department, 143 | email, 144 | phone 145 | )VALUES( 146 | 'Jonas', 147 | 'Salk', 148 | 5, 149 | 'Biology', 150 | 'jsalk@school.org', 151 | 777-555-4321 152 | ); 153 | 154 | 155 | -------------------------------------------------------------------------------- /DISTINCT.sql: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT name FROM customers, /*DISTINCT keyword is used to remove redundancies*/ 2 | SELECT DISTINCT zip FROM customers, 3 | SELECT DISTINCT city FROM customers -------------------------------------------------------------------------------- /Excercises.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM cd.facilities; 2 | 3 | SELECT name, membercost FROM cd.facilities; 4 | 5 | SELECT * FROM cd.facilities 6 | WHERE membercost != 0; 7 | 8 | SELECT facid, name, membercost, monthlymaintenance 9 | FROM cd.facilities 10 | WHERE (membercost < (monthlymaintenance * 0.02) AND membercost != 0); 11 | 12 | SELECT * FROM cd.facilities 13 | WHERE name LIKE '%Tennis%'; 14 | 15 | SELECT * FROM cd.facilities 16 | WHERE (facid = 1 OR facid = 5); 17 | 18 | SELECT memid, surname, firstname, joindate 19 | FROM cd.members 20 | WHERE (joindate > '2012-09-1') 21 | 22 | SELECT DISTINCT surname FROM cd.members 23 | ORDER BY surname ASC LIMIT 10; 24 | 25 | SELECT joindate FROM cd.members 26 | ORDER BY memid 27 | DESC LIMIT 1; 28 | 29 | SELECT COUNT(*) FROM cd.facilities 30 | WHERE guestcost >= 10; 31 | 32 | SELECT cd.facilities.facid, SUM(cd.facilities.facid) FROM cd.facilities 33 | INNER JOIN cd.bookings 34 | ON cd.bookings.facid = cd.facilities.facid 35 | INNER JOIN cd.members 36 | ON cd.members.memid = cd.bookings.memid 37 | WHERE cd.members.joindate BETWEEN '2012-09-01' AND '2012-10-01' 38 | GROUP BY cd.facilities.facid 39 | ORDER BY SUM(cd.facilities.facid) DESC; 40 | 41 | SELECT facid, SUM(slots) FROM cd.bookings 42 | GROUP BY facid 43 | HAVING SUM(slots) > 1000 44 | ORDER BY facid; 45 | 46 | SELECT starttime, name 47 | FROM cd.bookings 48 | INNER JOIN cd.facilities 49 | ON cd.bookings.facid = cd.facilities.facid 50 | WHERE name IN ('Tennis Court 1', 'Tennis Court 2') AND starttime >= '2012-09-21' 51 | ORDER BY starttime; 52 | 53 | 54 | SELECT starttime FROM cd.bookings 55 | INNER JOIN cd.members 56 | ON cd.bookings.memid = cd.members.memid 57 | WHERE firstname = 'David' AND surname = 'Farrell'; 58 | 59 | -------------------------------------------------------------------------------- /FUNCTIONS.sql: -------------------------------------------------------------------------------- 1 | /*-------------- FUNCTIONS-----------------*/ 2 | SELECT name,UPPER(name) FROM customers, 3 | SELECT ABS(cost) FROM items, 4 | SELECT cost,SQRT(cost) AS square_root FROM items, 5 | SELECT SUM(bids) FROM items, 6 | SELECT AVG(cost) FROM items, 7 | SELECT ROUND(SQRT(cost),4) FROM items ORDER BY cost DESC, 8 | SELECT MAX(cost) FROM items, 9 | SELECT MIN(cost) FROM items, 10 | SELECT COUNT(name) FROM items WHERE seller_id=6, 11 | SELECT COUNT(*) AS item_count, MAX(cost) AS max_cost, AVG(cost) AS avg_cost, WHERE seller_id=12; 12 | SELECT MOD(18, 4); 13 | SELECT RIGHT('SQL Tutorial', 3) AS ExtractString; -------------------------------------------------------------------------------- /Full Outer Join.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM customer 2 | FULL OUTER JOIN payment 3 | ON customer.customer_id = payment.customer_id 4 | WHERE customer.customer_id IS null OR payment.customer_id IS null; 5 | 6 | 7 | -------------------------------------------------------------------------------- /GROUPBY.sql: -------------------------------------------------------------------------------- 1 | SELECT staff_id, COUNT(amount) 2 | FROM payment 3 | GROUP BY staff_id 4 | ORDER BY COUNT(amount) DESC 5 | LIMIT 2; 6 | 7 | SELECT rating, AVG(replacement_cost) 8 | FROM film 9 | GROUP BY rating; 10 | 11 | SELECT customer_id, SUM(amount) 12 | FROM payment 13 | GROUP BY customer_id 14 | ORDER BY SUM(amount) DESC 15 | LIMIT 5; 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /HAVING.sql: -------------------------------------------------------------------------------- 1 | SELECT customer_id, COUNT(amount) 2 | FROM payment 3 | GROUP BY customer_id 4 | HAVING COUNT(amount) >= 40; 5 | 6 | SELECT customer_id, SUM(amount) 7 | FROM payment 8 | WHERE staff_id = 2 9 | GROUP BY customer_id 10 | HAVING SUM(amount) > 100; 11 | 12 | SELECT customer_id, SUM(amount) 13 | FROM payment 14 | WHERE staff_id = 2 15 | GROUP BY customer_id 16 | HAVING SUM(amount) >= 110; 17 | -------------------------------------------------------------------------------- /Hackerrank SQL solutions/African Cities.sql: -------------------------------------------------------------------------------- 1 | SELECT city.name 2 | FROM city 3 | INNER JOIN country 4 | ON city.countrycode = country.code 5 | WHERE country.continent = 'Africa'; 6 | -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Average population of each continent.sql: -------------------------------------------------------------------------------- 1 | SELECT country.continent, FLOOR(AVG(city.population)) 2 | FROM city 3 | INNER JOIN country 4 | ON city.countrycode = country.code 5 | GROUP BY country.continent; 6 | -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Customer Spending.sql: -------------------------------------------------------------------------------- 1 | SELECT customer_name, CAST(invoice.total_price AS DECIMAL (10,6)) 2 | FROM customer 3 | INNER JOIN invoice 4 | ON customer.id = invoice.customer_id 5 | WHERE invoice.total_price <= (SELECT 0.25 * AVG(total_price) FROM invoice) 6 | GROUP BY customer.id, customer.customer_name, invoice.total_price 7 | ORDER BY invoice.total_price DESC; 8 | -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Invoices per country.sql: -------------------------------------------------------------------------------- 1 | SELECT country_name, COUNT(invoice.id), AVG(invoice.total_price) 2 | FROM country 3 | INNER JOIN city 4 | ON country.id = city.country_id 5 | INNER JOIN customer 6 | ON city.id = customer.city_id 7 | INNER JOIN invoice 8 | ON customer.id = invoice.customer_id 9 | GROUP BY country_name 10 | HAVING AVG(invoice.total_price) > (SELECT AVG(total_price) FROM invoice); 11 | -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Japanese Cities' Names.sql: -------------------------------------------------------------------------------- 1 | SELECT NAME FROM city WHERE (COUNTRYCODE = 'JPN'); -------------------------------------------------------------------------------- /Hackerrank SQL solutions/New Companies.sql: -------------------------------------------------------------------------------- 1 | SELECT company_code, founder, 2 | (SELECT COUNT(DISTINCT lead_manager_code) FROM Lead_Manager WHERE company_code = c.company_code), 3 | (SELECT COUNT(DISTINCT senior_manager_code) FROM Senior_Manager WHERE company_code = c.company_code), 4 | (SELECT COUNT(DISTINCT manager_code) FROM Manager WHERE company_code = c.company_code), 5 | (SELECT COUNT(DISTINCT employee_code) FROM Employee WHERE company_code = c.company_code) 6 | FROM Company c 7 | ORDER BY company_code ASC; 8 | -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Ollivander's Inventory.sql: -------------------------------------------------------------------------------- 1 | SELECT id, age, coins_needed, power 2 | FROM Wands AS W 3 | INNER JOIN Wands_Property AS P 4 | ON W.code = P.code 5 | WHERE is_evil = 0 AND coins_needed = (SELECT MIN(coins_needed) 6 | FROM Wands AS X 7 | INNER JOIN Wands_Property AS Y 8 | ON X.code = Y.code 9 | WHERE X.power = W.power AND Y.age = P.age) 10 | ORDER BY power DESC, age DESC; 11 | -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Placements.sql: -------------------------------------------------------------------------------- 1 | SELECT Name 2 | FROM Students 3 | INNER JOIN Friends 4 | ON Students.ID = Friends.ID 5 | INNER JOIN Packages 6 | ON Friends.Friend_ID = Packages.ID 7 | INNER JOIN Packages P1 8 | ON P1.ID = Students.ID 9 | WHERE Packages.Salary > P1.Salary 10 | ORDER BY Packages.Salary; 11 | -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Revising the Select Query I.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM city WHERE (COUNTRYCODE = 'USA' AND POPULATION > 100000); -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Revising the Select Query II.sql: -------------------------------------------------------------------------------- 1 | SELECT NAME FROM city WHERE (COUNTRYCODE = 'USA' AND POPULATION > 120000); -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Select All.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM city; -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Select By ID.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM city WHERE (ID = 1661); -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Weather Observation Station 1.sql: -------------------------------------------------------------------------------- 1 | SELECT CITY, STATE FROM station; -------------------------------------------------------------------------------- /Hackerrank SQL solutions/Weather Observation Station 3.sql: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT CITY FROM station WHERE (MOD(ID, 2) = 0); -------------------------------------------------------------------------------- /Hackerrank SQL solutions/japancities.sql: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN. 4 | 5 | */ 6 | 7 | SELECT * FROM city WHERE (COUNTRYCODE = 'JPN'); -------------------------------------------------------------------------------- /Hackerrank SQL solutions/population census.sql: -------------------------------------------------------------------------------- 1 | SELECT SUM(city.population) 2 | FROM city 3 | INNER JOIN country 4 | ON city.countrycode = country.code 5 | WHERE country.continent = "Asia"; 6 | -------------------------------------------------------------------------------- /INNOTIN.sql: -------------------------------------------------------------------------------- 1 | SELECT id,name,city,state FROM customers WHERE city IN('CA','NC','NY') ORDER BY city DESC, /*IN statement basically combines all the OR statements together* 2 | SELECT id,name,city FROM customers WHERE city='CA' OR city='NY' OR city='NC' ORDER BY city /*Rather than using a bunch of "OR" statements we use IN keyword */ 3 | SELECT id,name,city FROM customers WHERE city NOT IN('CA','NY','NC') -------------------------------------------------------------------------------- /Inner Join.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM payment 2 | INNER JOIN customer 3 | ON customer.customer_id = payment.customer_id; 4 | 5 | SELECT payment_id, payment.customer_id, first_name 6 | FROM payment 7 | INNER JOIN customer 8 | ON customer.customer_id = payment.customer_id; 9 | 10 | 11 | SELECT email FROM customer 12 | INNER JOIN address 13 | ON customer.address_id = address.address_id 14 | WHERE address.district = 'California'; 15 | 16 | SELECT actor_id, first_name, last_name FROM actor 17 | WHERE first_name = 'Nick' AND last_name = 'Wahlberg'; 18 | 19 | SELECT title FROM film 20 | INNER JOIN film_actor 21 | ON film.film_id = film_actor.film_id 22 | WHERE film_actor.actor_id = 2; 23 | 24 | SELECT title, first_name, last_name FROM actor 25 | INNER JOIN film_actor 26 | ON actor.actor_id = film_actor.actor_id 27 | INNER JOIN film 28 | ON film.film_id = film_actor.film_id 29 | WHERE first_name = 'Nick' 30 | AND last_name = 'Wahlberg'; 31 | 32 | 33 | SELECT title FROM film 34 | INNER JOIN inventory 35 | ON film.film_id = inventory.film_id 36 | INNER JOIN rental 37 | ON inventory.inventory_id = rental.inventory_id 38 | WHERE rental.return_date 39 | BETWEEN '2005-05-29' AND '2005-05-30'; 40 | -------------------------------------------------------------------------------- /LIMIT.sql: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT id,name,state FROM customers LIMIT 5, /*LIMIT keyword is used to limit the search results*/ 2 | SELECT name,city,state FROM customers LIMIT 10,10 /*Start from 10th result and go down more 10*/ -------------------------------------------------------------------------------- /Left Outer Join.sql: -------------------------------------------------------------------------------- 1 | SELECT film.film_id, title, inventory_id, store_id 2 | FROM film 3 | LEFT OUTER JOIN inventory 4 | ON inventory.film_id = film.film_id 5 | WHERE inventory.film_id IS null; 6 | -------------------------------------------------------------------------------- /Math Operations.sql: -------------------------------------------------------------------------------- 1 | SELECT ROUND(rental_rate/replacement_cost, 2) FROM film; 2 | 3 | SELECT 0.1 * replacement_cost AS ten_percent 4 | FROM film; 5 | -------------------------------------------------------------------------------- /ORDERBY.sql: -------------------------------------------------------------------------------- 1 | SELECT id,name,cost FROM items ORDER BY name, 2 | SELECT id,name,zip FROM customers ORDER BY id,name, 3 | SELECT zip FROM customers ORDER BY zip DESC -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Everything-About-SQL 2 | Most of the things you need to know about SQL 3 | -------------------------------------------------------------------------------- /Right Outer Join.sql: -------------------------------------------------------------------------------- 1 | SELECT film.film_id, title, inventory_id, store_id 2 | FROM inventory 3 | RIGHT OUTER JOIN film 4 | ON film.film_id = inventory.film_id; 5 | -------------------------------------------------------------------------------- /SHOW_SELECT.sql: -------------------------------------------------------------------------------- 1 | SHOW DATABASES, /*Shows all the databases*/ 2 | SHOW TABLES, /*Shows all the tables*/ 3 | SELECT name FROM customers /*Selects name column from customers table*/ 4 | SELECT id FROM items, 5 | SELECT id,name,cost FROM items /*Multiple columns to select*/ -------------------------------------------------------------------------------- /SQL Practical/MongoDB dumpyard/mongodb1practical.txt: -------------------------------------------------------------------------------- 1 | Perform following MongoDB CRUD operations on Employee(empid,ename,city,salary,) 2 | 1. Insert 10 records in Employee collection 3 | 2. Display all employees staying in Mumbai 4 | 3. Display employees who stays in Pune and has salary less than 50000 5 | 4. Display employees who works in sales dept and stays in city Nasik or Nagpur. 6 | 5. Change the address of Employee “Ramesh” to Pune. 7 | 6. Create index on empid 8 | 7. Display employee having lowest salary. 9 | 8. Display first 5 highest paid employees 10 | 11 | use db 12 | db.employee.insert( 13 | [ 14 | { 15 | "id":1, 16 | "name":"pranay", 17 | "city":"pune", 18 | "salary":2000 19 | }, 20 | { 21 | "id":2, 22 | "name":"marques", 23 | "city":"pune", 24 | "salary":3000 25 | }, 26 | { 27 | "id":3, 28 | "name":"elon", 29 | "city":"mumbai", 30 | "salary":30000 31 | }, 32 | { 33 | "id":4, 34 | "name":"rajan", 35 | "city":"pune", 36 | "salary":3000 37 | }, 38 | { 39 | "id":5, 40 | "name":"mukesh", 41 | "city":"mumbai", 42 | "salary":4000 43 | }, 44 | { 45 | "id":6, 46 | "name":"Ramesh", 47 | "city":"pune", 48 | "salary":4000 49 | }, 50 | { 51 | "id":7, 52 | "name":"suresh", 53 | "city":"mumbai", 54 | "salary":500 55 | }, 56 | { 57 | "id":8, 58 | "name":"marlon", 59 | "city":"mumbai", 60 | "salary":300 61 | }, 62 | { 63 | "id":9, 64 | "name":"ruddu", 65 | "city":"pune", 66 | "salary":100 67 | }, 68 | { 69 | "id":10, 70 | "name":"rakesh", 71 | "city":"pune", 72 | "salary":2000 73 | } 74 | 75 | ] 76 | 77 | ) 78 | 79 | db.employee.find( 80 | 81 | {"city":"mumbai"} 82 | 83 | ).pretty() 84 | 85 | 86 | db.employee.find( 87 | 88 | { 89 | $and:[ 90 | 91 | {"city":"pune"}, 92 | {"salary": {$lt:50000}} 93 | 94 | ] 95 | 96 | } 97 | 98 | ) 99 | 100 | 101 | db.employee.update( 102 | {"id":6}, 103 | {"id":6, "name":"Ramesh", "city":"Mumbai", "salary":3000} 104 | ) 105 | 106 | db.employee.find().sort({"salary":1}).limit(1) 107 | 108 | 109 | db.employee.find().sort({"salary": -1}).limit(5) -------------------------------------------------------------------------------- /SQL Practical/MongoDB dumpyard/mongodb2practical.txt: -------------------------------------------------------------------------------- 1 | Perform MongoDB operations on product (pid, pname, quantity, price) 2 | 1. Insert 10 records in Product collection 3 | 2. Display products with prices above 4500 4 | 3. Display product having prices 1200 to 2500 5 | 4. Display product having highest price. 6 | 5. Sort all products according to price. 7 | 6. Create index on id field. 8 | 7. Display first record from dataset. 9 | 8. Delete product having pid=5 10 | 11 | 12 | use db 13 | db.product.insert( 14 | [ 15 | { 16 | "id":1, 17 | "name":"iPhone X", 18 | "quantity":2, 19 | "price":1200 20 | }, 21 | { 22 | "id":2, 23 | "name":"iPhone Xs", 24 | "quantity":3, 25 | "price":3000 26 | }, 27 | { 28 | "id":3, 29 | "name":"iPhone XsMax", 30 | "quantity":1, 31 | "price":1200 32 | }, 33 | { 34 | "id":4, 35 | "name":"iPad Pro", 36 | "quantity":10, 37 | "price":100000 38 | }, 39 | { 40 | "id":5, 41 | "name":"PocoPhone", 42 | "quantity":2, 43 | "price":800 44 | }, 45 | { 46 | "id":6, 47 | "name":"Files", 48 | "quantity":40, 49 | "price":250 50 | }, 51 | { 52 | "id":7, 53 | "name":"Pen", 54 | "quantity":100, 55 | "price":1000 56 | }, 57 | { 58 | "id":8, 59 | "name":"Stapler", 60 | "quantity":50, 61 | "price":700 62 | }, 63 | { 64 | "id":9, 65 | "name":"Liquid Soap", 66 | "quantity":5, 67 | "price":100 68 | }, 69 | { 70 | "id":10, 71 | "name":"Toothbrush", 72 | "quantity":2, 73 | "price":150 74 | } 75 | ] 76 | ) 77 | 78 | db.product.find( 79 | {"price":{$gt:4500}} 80 | ).pretty() 81 | 82 | 83 | db.product.find( 84 | $and[ 85 | {"price":{$gt:1200}}, 86 | {"price":{$lt:2500}} 87 | ] 88 | ).pretty() 89 | 90 | 91 | db.product.find().sort({"price":-1}).limit(1) 92 | 93 | 94 | db.product.find().sort({"price":1}) 95 | 96 | db.product.findOne() 97 | 98 | db.product.remove( 99 | {"id":5} 100 | ) -------------------------------------------------------------------------------- /SQL Practical/MongoDB dumpyard/mongodb3practical.txt: -------------------------------------------------------------------------------- 1 | Create Student Collection(rollno,name,marks,class,city,branch) using MongoDB 2 | 1. Find names of students who have obtained more than 300 marks 3 | 2. Display list of all students of Computer branch . 4 | 3. Find the first 5 toppers of TE 5 | 4. Sort the student names in descending order 6 | 5. Display only first 2 documents 7 | 6. Display all students who resides in Mumbai or Pune and has above 250 marks 8 | 7. Display only names and marks of each student 9 | 10 | use db 11 | db.student.insert( 12 | [ 13 | { 14 | "rollno":1, 15 | "name":"pranay", 16 | "marks":450, 17 | "class":"TE", 18 | "city":"pune", 19 | "branch":"computer" 20 | }, 21 | { 22 | "rollno":2, 23 | "name":"ruddu", 24 | "marks":500, 25 | "class":"SE", 26 | "city":"mumbai", 27 | "branch":"computer" 28 | }, 29 | { 30 | "rollno":3, 31 | "name":"bajare", 32 | "marks":300, 33 | "class":"TE", 34 | "city":"pune" 35 | "branch":"production" 36 | }, 37 | { 38 | "rollno":4, 39 | "name":"mohit", 40 | "marks":200, 41 | "class":"FE", 42 | "city":"delhi", 43 | "branch":"computer" 44 | }, 45 | { 46 | "rollno":5, 47 | "name":"hrishi", 48 | "marks":250, 49 | "class":"TE", 50 | "city":"LA", 51 | "branch":"electrical" 52 | } 53 | ] 54 | ) 55 | 56 | db.student.find( 57 | {"marks": {$gt:300}} 58 | ) 59 | 60 | db.student.find( 61 | {"branch": "computer"} 62 | ) 63 | 64 | db.student.find( 65 | {"class":"TE"} 66 | ).sort({"marks":-1}).limit(5) 67 | 68 | db.student.find().sort({"name":-1}) 69 | 70 | db.student.find().limit(2) 71 | 72 | db.student.find( 73 | 74 | { 75 | $or:[ 76 | {"city":"mumbai"}, 77 | {"city":"pune"} 78 | ]$and:{ 79 | {"marks":{$gt:250}} 80 | } 81 | 82 | } 83 | 84 | ) 85 | 86 | db.student.find( 87 | {"name":1, "marks:1", "_id":0} 88 | ).pretty() 89 | 90 | -------------------------------------------------------------------------------- /SQL Practical/MongoDB dumpyard/mongodb4practical.txt: -------------------------------------------------------------------------------- 1 | Implement product Collection using MongoDB and implement following queries. 2 | 1. Find all names of products having price above 10000 3 | 2. Sort the product names in descending order 4 | 3. Display all products having price above 10000 and manufactured in Mumbai 5 | 4. Display product having minimum price 6 | 5. Delete product having pid=5 7 | 6. Update price of Product “pencil” by 10 8 | 7. Create index on product collection 9 | 10 | use db 11 | db.product.insert( 12 | [ 13 | { 14 | "id":1, 15 | "name":"iPhone X", 16 | "quantity":2, 17 | "price":1200, 18 | "manufactured":"pune" 19 | }, 20 | { 21 | "id":2, 22 | "name":"iPhone Xs", 23 | "quantity":3, 24 | "price":3000, 25 | "manufactured":"LA" 26 | }, 27 | { 28 | "id":3, 29 | "name":"iPhone XsMax", 30 | "quantity":1, 31 | "price":1200, 32 | "manufactured":"China" 33 | }, 34 | { 35 | "id":4, 36 | "name":"iPad Pro", 37 | "quantity":10, 38 | "price":100000, 39 | "manufactured":"mumbai" 40 | }, 41 | { 42 | "id":5, 43 | "name":"PocoPhone", 44 | "quantity":2, 45 | "price":800, 46 | "manufactured":"pune" 47 | }, 48 | { 49 | "id":6, 50 | "name":"Files", 51 | "quantity":40, 52 | "price":250, 53 | "manufactured":"mumbai" 54 | }, 55 | { 56 | "id":7, 57 | "name":"Pencil", 58 | "quantity":100, 59 | "price":1000, 60 | "manufactured":"pune" 61 | }, 62 | { 63 | "id":8, 64 | "name":"Stapler", 65 | "quantity":50, 66 | "price":700, 67 | "manufactured":"mumbai" 68 | }, 69 | { 70 | "id":9, 71 | "name":"Liquid Soap", 72 | "quantity":5, 73 | "price":100, 74 | "manufactured":"pune" 75 | }, 76 | { 77 | "id":10, 78 | "name":"Toothbrush", 79 | "quantity":2, 80 | "price":150, 81 | "manufactured":"mumbai" 82 | } 83 | ] 84 | ) 85 | 86 | 87 | db.product.find( 88 | {"price":{$gt:10000}} 89 | ).pretty() 90 | 91 | 92 | db.product.find().sort({"name":-1}) 93 | 94 | 95 | db.product.find( 96 | { 97 | $and:[ 98 | {"manufactured":"mumbai"}, 99 | {"price":{$gt:10000}} 100 | ] 101 | } 102 | ) 103 | 104 | 105 | 106 | db.product.find().sort({"price":1}).limit(1) 107 | 108 | 109 | db.product.remove( 110 | {"id":5} 111 | ) 112 | 113 | 114 | db.product.update( 115 | {"id":7}, 116 | { 117 | "id":7, 118 | "name":"Pencil", 119 | "quantity":100, 120 | "price":100, 121 | "manufactured":"pune" 122 | } 123 | ) 124 | 125 | -------------------------------------------------------------------------------- /SQL Practical/MongoDB dumpyard/mongodb5practical.txt: -------------------------------------------------------------------------------- 1 | Create MongoDB collection to store details of Movies (name, director, type, likes, grade, collection ) and perform following queries: 2 | 1) Insert 10 documents in the collection 3 | 2) Display all the movies directed by a director ‘Karan’ 4 | 3) Display all the movies directed by ‘Karan’ or ‘Yash’ 5 | 4) Sort the movies based on the obtained likes received. 6 | 5) Display movies directed by ‘Abhinav’ having grade 1 or 2 7 | 6) Display movie name which were produce by ‘Prakash’ having more than 20000 likes 8 | 7) Display all Comedy movies names with their director names. 9 | 8) Calculate total collection of movies according to their directors 10 | 11 | use db 12 | db.movies.insert( 13 | [ 14 | 15 | 16 | ] 17 | ) 18 | 19 | db.movies.find( 20 | {"director":"karan"} 21 | ).pretty() 22 | 23 | db.movies.find( 24 | {$or:[ 25 | {"director":"karan"}, 26 | {"director":"yash"} 27 | ] 28 | } 29 | ) 30 | 31 | db.movies.find().sort({"likes": -1}) 32 | 33 | 34 | db.movies.find( 35 | 36 | { 37 | 38 | "director":"abhinav" 39 | }$and:[ 40 | { 41 | $or:[ 42 | {"grade":1}, 43 | {"grade":2} 44 | ] 45 | 46 | } 47 | 48 | ] 49 | 50 | ) 51 | 52 | 53 | db.movies.find( 54 | { 55 | $and:[ 56 | {"director":"prakash"}, 57 | {"likes":{$gt:20000}} 58 | ] 59 | } 60 | ) 61 | 62 | 63 | db.movies.find( 64 | {"type":"comedy"} 65 | {"director":1, "type":1, "_id":0} 66 | ).pretty() 67 | 68 | 69 | -------------------------------------------------------------------------------- /SQL Practical/MongoDB dumpyard/mongodb6practical.txt: -------------------------------------------------------------------------------- 1 | Create MongoDB collection to store details of Movies (name, director, type, likes, grade, collection ) and perform following queries: 2 | 1) Insert 10 documents in the collection 3 | 2) Display all the movies directed by ‘Karan’ or ‘Yash’ 4 | 3) Display top 5 movies based on likes received. 5 | 4) Display movie name which were produce by ‘Prakash’ having 5 grade 6 | 5) Display movies having grade less than 3 7 | 6) Display movies names in proper order. 8 | 7) Calculate total collection of movies according to their directors 9 | 8) Delete all movies having grade ‘1’ 10 | 11 | use db 12 | db.movies.insert([ 13 | { 14 | 15 | } 16 | 17 | ]) 18 | 19 | 20 | db.movies.find( 21 | { 22 | $or:[ 23 | {"director":"karan"}, 24 | {"director":"yash"} 25 | ] 26 | } 27 | ) 28 | 29 | 30 | db.movies.find().sort({"likes": -1}).limit(5) 31 | 32 | 33 | db.movies.find( 34 | { 35 | $and:[ 36 | {"director":"prakash"}, 37 | {"grade":5} 38 | ] 39 | } 40 | ) 41 | 42 | 43 | db.movies.find( 44 | {"grade":{$lt:3}} 45 | ).pretty() 46 | 47 | 48 | db.movies.find().sort({"name":1}) 49 | 50 | db.movies.remove( 51 | {"grade":1} 52 | ) 53 | 54 | -------------------------------------------------------------------------------- /SQL Practical/MySQL dumpyard/mysql1.txt: -------------------------------------------------------------------------------- 1 | Create a database COMPANY consisting of two tables EMP & DEPT 2 | EMP: 3 | EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO 4 | DEPT: 5 | DEPTNO, DNAME, LOCATION 6 | 7 | 1. Create database.Create above tables, set appropriate Primary Keys. 8 | 2. Add 10 rows to both the tables. 9 | 3. Add one new column: mobile_no to EMP table. 10 | 4. Delete the Employee 7369. 11 | 5. Increase the salary of 7499 by 10% 12 | 6. Truncate Department Table. 13 | 7. Create a view that holds data of only manager 14 | 8. Display Employee names with their Department Names. 15 | 16 | CREATE DATABASE company; 17 | CREATE TABLE employee( 18 | id int(11), 19 | name varchar(30), 20 | job varchar(30), 21 | mgr int(11), 22 | hiredate date, 23 | sal int(11), 24 | deptno int(11), 25 | PRIMARY KEY(id) 26 | ); 27 | 28 | CREATE TABLE dept( 29 | deptno int(11), 30 | name varchar(30), 31 | location varchar(30), 32 | PRIMARY KEY(deptno) 33 | ); 34 | 35 | 36 | INSERT INTO employee VALUES(); 37 | INSERT INTO dept VALUES(); 38 | 39 | ALTER TABLE employee ADD COLUMN(mobile_no int(11)); 40 | 41 | DELETE FROM employee WHERE id = 7369; 42 | 43 | UPDATE employee SET sal=sal+(sal/100*10) WHERE id = 7499; 44 | 45 | TRUNCATE TABLE dept; 46 | 47 | CREATE VIEW manager_records 48 | AS SELECT * FROM employee 49 | WHERE job="manager"; 50 | 51 | SELECT e.name,d.name FROM employee e, dept d WHERE e.deptno = d.deptno; 52 | 53 | -------------------------------------------------------------------------------- /SQL Practical/MySQL dumpyard/mysql2.txt: -------------------------------------------------------------------------------- 1 | Design following queries in MYSQL: 2 | Employee(ename,ssn,dateofbirth,city,salary,dno,managerid) 3 | Department(dname,dno) 4 | Dept_locations(dno,dcity) 5 | Project(pname,pnumber,dno) 6 | 1) Create following tables with appropriate primary keys. 7 | 2) Insert 5 records in each table. 8 | 3) Retrieve the names of employees in department 5 who works on ‘product x’ project. 9 | 10 | 4) List the names of employees who have dependent with the same first name as themselves. 11 | 5) Find the names of employees that are managed by ‘smith’. 12 | 6) For each project, list the project name and department name in which that project is assigned 13 | 7) Retrieve the names of departments which are located at Mumbai. 14 | 15 | 16 | CREATE TABLE employee( 17 | name varchar(30), 18 | ssn int(11), 19 | doB date, 20 | city varchar(30), 21 | salary int(11), 22 | dno int(11), 23 | managerid int(11), 24 | PRIMARY KEY(ssn) 25 | ); 26 | 27 | CREATE TABLE department( 28 | name varchar(30), 29 | dno int(11), 30 | PRIMARY KEY(dno) 31 | ); 32 | 33 | CREATE TABLE dept_locations( 34 | dno int(11), 35 | city varchar(30), 36 | PRIMARY KEY(dno) 37 | ); 38 | 39 | CREATE TABLE project( 40 | name varchar(30), 41 | pno int(11), 42 | dno int(11), 43 | PRIMARY KEY(pno) 44 | ); 45 | 46 | INSERT INTO employee VALUES(); 47 | INSERT INTO department VALUES(); 48 | INSERT INTO dept_locations VALUES(); 49 | INSERT INTO project VALUES(); 50 | 51 | SELECT e.name, e.dno, p.name FROM employee e, project p WHERE e.dno = 5 AND p.name="product x"; 52 | 53 | SELECT name FROM employee WHERE managerid=1; 54 | 55 | SELECT p.name,d.name FROM project p, department d WHERE p.dno = d.dno; 56 | 57 | SELECT e.name, dl.city FROM employee e, dept_locations dl WHERE dl.city="mumbai"; 58 | -------------------------------------------------------------------------------- /SQL Practical/MySQL dumpyard/mysql3and4.txt: -------------------------------------------------------------------------------- 1 | Using MySQL /JDBC, Create table Employee and Perform insert, update and delete table operations. 2 | 3 | Using MySQL /JDBC, Create table Books and Perform insert, update and delete table operations. 4 | 5 | CREATE TABLE employee( 6 | id int(11) NOT NULL, 7 | name varchar(30) NOT NULL, 8 | dept varchar(30), 9 | email varchar(30), 10 | city varchar(30), 11 | salary int(11), 12 | PRIMARY KEY(id) 13 | ); 14 | 15 | INSERT INTO employee VALUES(); 16 | 17 | UPDATE employee SET salary=20000 WHERE id=1; 18 | 19 | DELETE FROM employee; 20 | -------------------------------------------------------------------------------- /SQL Practical/MySQL dumpyard/mysql5.txt: -------------------------------------------------------------------------------- 1 | Create a table EMP:EMPNO, ENAME, JOB, DESG, HIREDATE, SAL, DEPTNO 2 | 3 | 1. Create table , set appropriate Primary Key. Add 10 rows to the table. 4 | 2. Display different types of designations 5 | 3. Write a query to display the name and salary for all employees whose salary is not in the range 10,000 through 45,000 and are in department 30 or 100. 6 | 4. Increase the salary of 7499 by 10% 7 | 5. Create a view that holds data of employee and their salaries with their designation 8 | 6. Display Employee names with their Departments. 9 | 7. Display employees whose name has ‘a’ in it. 10 | 11 | CREATE TABLE employee( 12 | id int, 13 | name varchar(30), 14 | job varchar(30), 15 | desg varchar(30), 16 | hiredate date, 17 | salary int, 18 | deptno int, 19 | PRIMARY KEY(id) 20 | ); 21 | 22 | INSERT INTO employee VALUES(); 23 | 24 | SELECT DISTINCT desg FROM employee; 25 | 26 | SELECT name,salary FROM employee WHERE salary NOT BETWEEN 10000 AND 45000 AND deptno IN(30,100); 27 | 28 | UPDATE employee SET salary = salary+(salary/100*10) WHERE id = 7499; 29 | 30 | CREATE VIEW record AS SELECT id,name,job,salary,desg FROM employee; 31 | 32 | SELECT name,deptno FROM employee; 33 | 34 | SELECT name FROM employee WHERE name LIKE "%a%"; 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /SQL Practical/MySQL dumpyard/mysql6.txt: -------------------------------------------------------------------------------- 1 | Create a table EMP:EMPNO, ENAME,MGR, HIREDATE, SAL, DEPTNAME 2 | 3 | 1. Create table , set appropriate Primary Key.Add 10 rows to the table. 4 | 2. Display employee who joined in December 2017. 5 | 3. Display employees staying in Pune having salary greater than 45000. 6 | 4. Modify deptname of “Smith ” to “Research” 7 | 5. Display the highest paid employee in each dept 8 | 6. Display average salary of “Sales” dept 9 | 7. Display employees whose name starts with A. 10 | 11 | CREATE TABLE employee( 12 | id int, 13 | name varchar(30), 14 | mgr int, 15 | hiredate date, 16 | sal int, 17 | deptname varchar(30), 18 | PRIMARY KEY(id) 19 | ); 20 | 21 | INSERT INTO employee VALUES(); 22 | 23 | SELECT * FROM employee WHERE YEAR(hiredate)="2017"; 24 | 25 | UPDATE employee SET deptname="Research" WHERE deptname="Smith"; 26 | 27 | SELECT MAX(sal) FROM employee GROUP BY deptname; 28 | 29 | SELECT AVG(sal) FROM employee WHERE deptname="sales"; 30 | 31 | SELECT name FROM employee WHERE name LIKE "A%"; 32 | 33 | -------------------------------------------------------------------------------- /SQL Practical/MySQL dumpyard/mysql7.txt: -------------------------------------------------------------------------------- 1 | Create table Employee (empid, ename, salary, dept, city, dateofjoining) and perform following queries. 2 | 1) Create empid as primary key 3 | 2) Create user defined index on any column. 4 | 3) Display those employees who get more salary than the salary of Employee id ‘101’ 5 | 4) Increase salary of all employees by 5% 6 | 5) display all the employee in sorted order of their name 7 | 6) Calculate total number of employees in each department 8 | 7) calculate average salary of each department 9 | 10 | CREATE TABLE employee( 11 | id int, 12 | name varchar(30), 13 | salary int, 14 | dept int, 15 | city varchar(30), 16 | dateofjoining date, 17 | PRIMARY KEY(id) 18 | ); 19 | 20 | 21 | CREATE INDEX idx ON employee(id); 22 | //CREATE INDEX index_name ON table_name(col1, col2...coln); 23 | 24 | SELECT * FROM employee WHERE salary > (SELECT salary FROM employee WHERE id=101); 25 | 26 | UPDATE employee SET salary= salary+(salary/5*100); 27 | 28 | SELECT * FROM employee ORDER BY name; 29 | SELECT * FROM employee ORDER BY name DESC; 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /SQL Practical/MySQL dumpyard/mysql8.txt: -------------------------------------------------------------------------------- 1 | Create table Employee (empid, ename, salary, ,job,dept, city, dateofjoining) and perform following queries. 2 | 1) Create empid as primary key 3 | 2) Display employee names and id of all employees whose job is “Programmer” or “Manager” and salary not equals to 10000,20000 and 50000 4 | 3) List all the employees who are joined in July. 5 | 4) Display those employees who get more salary than the salary of Employee “Amar” 6 | 5) Increase salary of all employees by 5% 7 | 6) Calculate average salary of each department 8 | 7) Display employee name and their job who stays in city name starts with “N” 9 | 10 | 11 | CREATE TABLE employee( 12 | id int, 13 | name varchar(30), 14 | salary int, 15 | job varchar(30), 16 | dept varchar(30), 17 | city varchar(30), 18 | doj date, 19 | PRIMARY KEY(id) 20 | ); 21 | 22 | 23 | SELECT name,id FROM employee WHERE salary NOT IN(10000,20000,50000) AND job="Programmer" OR job="Manager"; 24 | 25 | SELECT * FROM employee WHERE MONTH(doj)=7; 26 | 27 | SELECT * FROM employee WHERE salary > (SELECT salary FROM employee WHERE name="Amar"); 28 | 29 | UPDATE employee SET salary = salary+(salary/100*5); 30 | 31 | SELECT name,job FROM employee WHERE city LIKE "N%"; 32 | 33 | -------------------------------------------------------------------------------- /SQL Practical/PLSQL dumpyard/plsql1.txt: -------------------------------------------------------------------------------- 1 | Q.Write a trigger to check salary during every insert to employee table if salary<5000 and if salary>5000 then display exception msg. 2 | 3 | **Make Table emp with eno,name,salary** 4 | **Make Table tracking with eno,name,salary 5 | *******************Program********************* 6 | set serveroutput on; 7 | create or replace trigger etrig 8 | before insert or update of salary on emp 9 | for each row 10 | declare 11 | msal number(10); 12 | begin 13 | msal:=:new.salary; 14 | if msal<50000 then 15 | raise_application_error(-200003,'Salary cannot be less than 50000'); 16 | else 17 | insert into tracking values(:new.eno,:new.salary); 18 | dbms_output.put_line('Updation Successful'); 19 | end if; 20 | end; 21 | / -------------------------------------------------------------------------------- /SQL Practical/PLSQL dumpyard/plsql2.txt: -------------------------------------------------------------------------------- 1 | Write a PL/SQL block of code for the following requirements:- 2 | Schema: 3 | 1. Borrower(Rollin, Name, DateofIssue, NameofBook, Status) 4 | 2. Fine(Roll_no,Date,Amt) 5 | Accept roll_no & name of book from user. 6 | Check the number of days (from date of issue), if days are between 15 to 30 then fine amount will be Rs 5per day. 7 | If no. of days>30, per day fine will be Rs 50 per day & for days less than 30, Rs. 5 per day. 8 | After submitting the book, status will change from I to R. 9 | If condition of fine is true, then details will be stored into fine table. 10 | 11 | 12 | ************************************************************************* 13 | set serveroutput on; 14 | declare 15 | c_rollno Mohit.rollno%type:=&c_rollno; 16 | c_bookname Mohit.bookname%type:=&c_bookname; 17 | c_fine Fine.fine%type; 18 | c_issuedate Mohit.doi%type; 19 | c_nodays number(4); 20 | 21 | begin 22 | select doi into c_issuedate from Mohit where rollno=c_rollno AND bookname=c_bookname; 23 | c_nodays:=sysdate-to_date(c_issuedate); 24 | if(c_nodays>15 and c_nodays<30) then 25 | c_fine:=5*c_nodays; 26 | elsif(c_nodays>30) then 27 | c_fine:=150+(c_nodays-30)*50; 28 | end if; 29 | if(c_fine>0) then 30 | insert into Fine values(c_rollno,sysdate,c_fine); 31 | end if; 32 | update Mohit set status='R' where rollno=c_rollno; 33 | 34 | 35 | exception 36 | when no_data_found then 37 | dbms_output.put_line('Incorrect RollNo or Bookname entered'); 38 | end; 39 | / 40 | -------------------------------------------------------------------------------- /SQL Practical/PLSQL dumpyard/plsql3.txt: -------------------------------------------------------------------------------- 1 | Write a Stored Procedure namely proc_Grade for the categorization of student. If marks scored by students in examination is <=1500 and marks>=990 then student will be placed in distinction category if marks scored are between 989 and900 category is first class, if marks 899 and 825 category is Higher Second Class .Write a PL/SQL block for using procedure created with above requirement. Stud_Marks(name, total_marks) Result(Roll,Name, Class) . 2 | 3 | *****************************************************8 4 | set serveroutput on; 5 | create or replace procedure proc_grade3(roll number,name varchar2,total_marks number) 6 | 7 | is class varchar(20); 8 | begin 9 | if(total_marks<=1500 AND total_marks>=990) then 10 | class:='Distinction'; 11 | elsif(total_marks<=989 AND total_marks>=900) then 12 | class:='First class'; 13 | elsif(total_marks<=899 AND total_marks>=825) then 14 | class:='Higher Second Class'; 15 | end if; 16 | insert into Stud_Marks1 values(name,total_marks); 17 | insert into Result1 values(roll,name,class); 18 | end; -------------------------------------------------------------------------------- /SQL Practical/PLSQL dumpyard/plsql4.txt: -------------------------------------------------------------------------------- 1 | Q.Write a database trigger on Library table. The System should keep track of the records that are being updated or deleted. The old value of updated or deleted records should be added in Library_Audit table 2 | 3 | set serveroutput on; 4 | create or replace trigger libtrig 5 | after update or delete of id on libr 6 | for each row 7 | begin 8 | insert into Library_Audit values(:old:id,:old:name) 9 | end; 10 | / -------------------------------------------------------------------------------- /SQL Practical/PLSQL dumpyard/plsql5.txt: -------------------------------------------------------------------------------- 1 | Write a PL/SQL block for following requirement and handle the exceptions. 2 | Roll no. of student will be entered by user. Attendance of roll no. entered by user will be checked in Stud table. If attendance is less than 75% then display the message “Term not granted” and set the status in stud table as “D”. Otherwise display message “Term granted” and set the status in stud table as “ND”. 3 | ************************************************* 4 | set serveroutput on; 5 | declare 6 | Cursor crsr_att is select r_no,att,status from stud where att<75; 7 | mroll stud.r_no%type; 8 | matt stud.att%type; 9 | mstatus stud.status%type; 10 | begin 11 | open crsr_att; 12 | if crsr_att%isopen then 13 | loop 14 | fetch crsr_att into mroll,matt,mstatus; 15 | exit when crsr_att%notfound; 16 | if crsr_att%found then 17 | update stud set status='D' where r_no=mroll; 18 | insert into d_stud values(mroll,matt); 19 | end if; 20 | end loop; 21 | end if; 22 | end; 23 | / -------------------------------------------------------------------------------- /SQL Practical/PLSQL dumpyard/plsql6.txt: -------------------------------------------------------------------------------- 1 | Write a PL/SQL block of code using parameterized Cursor, that will merge the data available in the newly created table N_RollCall with the data available in the table O_RollCall. If the data in the 2 | 3 | ********************************************************* 4 | set serveroutput on; 5 | declare 6 | cursor crsr_class is select * from O_RollCall; 7 | cursor crsr_chk(str_name varchar) is select roll from N_RollCall where name = str_name; 8 | str_roll N_RollCall.roll%type; 9 | str_name N_RollCall.name%type; 10 | v varchar(10); 11 | begin 12 | open crsr_class; 13 | loop 14 | fetch crsr_class into str_roll,str_name; 15 | exit when crsr_class%NOTFOUND; 16 | open crsr_chk(str_name); 17 | fetch crsr_chk into v; 18 | if crsr_chk%FOUND then 19 | dbms_output.put_line('student'||str_name||'exist'); 20 | else 21 | dbms_output.put_line('student'||str_name||'not exist. Inserting in N_RollCall table'); 22 | insert into N_RollCall values(str_roll,str_name); 23 | end if; 24 | close crsr_chk; 25 | end loop; 26 | close crsr_class; 27 | end; 28 | / -------------------------------------------------------------------------------- /SQL Practical/PLSQL dumpyard/plsql7.txt: -------------------------------------------------------------------------------- 1 | Write trigger which will not allow inserting on Monday, updating on Tuesday, deleting on Friday 2 | *************************************** 3 | 4 | CREATE OR REPLACE TRIGGER trig1 5 | BEFORE INSERT or UPDATE or DELETE ON employees 6 | for each row 7 | BEGIN 8 | IF inserting then 9 | IF TO_CHAR(SYSDATE,'DY') IN ('MON') THEN 10 | RAISE_APPLICATION_ERROR(-20500,'You cannot insert into the table on Monday'); 11 | END IF; 12 | IF updating then 13 | IF TO_CHAR(SYSDATE,'DY') IN ('TUE') THEN 14 | RAISE_APPLICATION_ERROR(-20501,'You cannot update into the table on Tuesday'); 15 | END IF; 16 | IF deleting then 17 | IF TO_CHAR(SYSDATE,'DY') IN ('FRI') THEN 18 | RAISE_APPLICATION_ERROR(-20502,'You cannot delete from the table on friday'); 19 | END IF; 20 | END; -------------------------------------------------------------------------------- /Self Join.sql: -------------------------------------------------------------------------------- 1 | SELECT f1.length, f1.title AS movie, f2.title, f2.length 2 | FROM film f1 3 | JOIN film f2 4 | ON f1.length = f2.length 5 | AND f1.film_id != f2.film_id; 6 | -------------------------------------------------------------------------------- /String Operations.sql: -------------------------------------------------------------------------------- 1 | SELECT first_name || ' ' || last_name AS name 2 | FROM customer 3 | 4 | SELECT left(first_name,1) || last_name || '@gmail.com' 5 | FROM customer 6 | -------------------------------------------------------------------------------- /Subquery.sql: -------------------------------------------------------------------------------- 1 | SELECT first_name, last_name 2 | FROM customer AS c 3 | WHERE EXISTS 4 | (SELECT * FROM payment AS p WHERE p.customer_id = c.customer_id AND amount>11); 5 | 6 | SELECT title FROM film 7 | WHERE rental_rate > (SELECT AVG(rental_rate) FROM film); 8 | -------------------------------------------------------------------------------- /UPDATE_DELETE.sql: -------------------------------------------------------------------------------- 1 | UPDATE items SET name='puddinghammock' WHERE id=106; 2 | UPDATE items SET name='frog paste',bids=66 WHERE id=106; /*UPDATE items/EDIT items*/ 3 | 4 | DELETE FROM items WHERE id=106; /*DELETE stuff from table*/ -------------------------------------------------------------------------------- /WHERE.sql: -------------------------------------------------------------------------------- 1 | SELECT id,name FROM customers WHERE id=6, 2 | SELECT name,zip FROM customers WHERE name='pranay', 3 | SELECT id,city,state FROM customers WHERE id<6, 4 | SELECT id,city,state FROM customers WHERE id!=6, 5 | SELECT id,city,state FROM customers WHERE id>6, 6 | SELECT id,city,state FROM customers WHERE id<=6, 7 | SELECT id,city,state FROM customers WHERE id>=6, 8 | SELECT id,city,state FROM customers WHERE id BETWEEN 25 AND 30, 9 | SELECT name,state,city FROM customers WHERE state='CA' AND city='Hollywood', 10 | SELECT name,state,city FROM customers WHERE state='MH' OR city='Boston', 11 | SELECT id,name,city FROM customers WHERE (id=1 OR id=2) AND city='Raleigh' /*MySQL works inside out!*/ 12 | 13 | 14 | -------------------------------------------------------------------------------- /WILDCARDS.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM customers /*Asterisk means everything*/ 2 | SELECT * FROM items 3 | SELECT name FROM items WHERE name LIKE 'new%' /*%- means anything*/ 4 | SELECT name FROM items WHERE name LIKE '%computer' 5 | SELECT name FROM items WHERE name LIKE '%boxes of frogs' 6 | SELECT name FROM items WHERE name LIKE 'h%d' 7 | SELECT name FROM items WHERE name LIKE '_boxes of frogs' /*_ means only a single character*/ -------------------------------------------------------------------------------- /custom_columns.sql: -------------------------------------------------------------------------------- 1 | SELECT CONCAT(city,', ',state) FROM customers 2 | SELECT CONCAT(city,', ',state) AS proper_address FROM customers 3 | 4 | SELECT name,cost,ROUND(cost-1,4) AS discount FROM items -------------------------------------------------------------------------------- /table_creation.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE customers( 2 | id int(11) NOT NULL AUTO_INCREMENT, 3 | name varchar(60) NOT NULL, 4 | address varchar(100) NOT NULL, 5 | city varchar(20), 6 | state varchar(30), 7 | zip int(11) NOT NULL, 8 | PRIMARY KEY(id) 9 | ) 10 | 11 | INSERT INTO customers(id,name,address,city,state,zip) VALUES 12 | (1,'pranay pathole','32 hungerford ave','Adams','NY',12478) 13 | (2, 'Noah Parker', '342 Tunafish Lane', 'Raleigh', 'NC', 27606), 14 | (3, 'Kelsey Burger', '43 Crab Orchard Dr', 'Oakland', 'CA', 90210), 15 | (4, 'Corey Smith', '423 Wisteria Lane', 'Simmersville', 'AK', 54112), 16 | (5, 'Harry Potter', '673 Greenwich Ave', 'Newark', 'NJ', 80432), 17 | (6, 'Henry Jackson', '2134 Grant St', 'Gary', 'IN', 47404), 18 | (7, 'Cynthia Alvarez', '1568 Greenfield Ave', 'Augusta', 'GA', 30568), 19 | (8, 'Teresa Smith', '8725 Black St', 'Philadelphia', 'PA', 19603), 20 | (9, 'Gary Foster', '3752 Wittfield Blvd', 'Indianapolis', 'IN', 46219), 21 | (10, 'Sherry Gibbons', '5517 Oak St', 'Phoenix', 'AZ', 85072), 22 | (11, 'Jack Nicholson', '1234 Star Lane', 'Hollywood', 'CA', 90210), 23 | (12, 'Penny Green', '4972 Massachusetts Ave', 'Orlando', 'FL', 33718), 24 | (13, 'Alexander Evans', '6310 E 21st Rd', 'Las Vegas', 'NV', 83594), 25 | (14, 'Jeremy White', '3954 Brentwood Dr', 'Seattle', 'WA', 99037), 26 | (15, 'Omar Badshah', '6801 Regina Cir', 'Madison', 'WI', 53209), 27 | (16, 'Preston Harrison', '104 Main St', 'Denver', 'CO', 81712), 28 | (17, 'Manuel Rodriguez', '99543 Westin Blvd', 'Provo', 'UT', 85478), 29 | (18, 'David Jones', '5488 W 34th St', 'Boston', 'MA', 2104), 30 | (19, 'Nick Flanders', '3486 Happy Trails Dr', 'Springfield', 'OH', 45872), 31 | (20, 'Paul Brown', '3290 Pennsylvania Ave', 'Chicago', 'IL', 61208), 32 | (21, 'Sara Rehm', '7746 Wysong Ave', 'Detroit', 'MI', 48913), 33 | (22, 'Haley Carter', '2957 Princess Way', 'Portland', 'OR', 97532), 34 | (23, 'Julian Thomas', '5564 Dandy Trail', 'Santa Fe', 'NM', 81543), 35 | (24, 'George Lucas', '8224 Star Wars Ln', 'Galaxy', 'TN', 37512), 36 | (25, 'Perry Jordan', '4321 Victory Rd', 'Montgomery', 'AL', 33219), 37 | (26, 'Christopher Layton', '871 Georgia Pl', 'Houston', 'TX', 78704), 38 | (27, 'Devon Myers', '9932 Carmel Dr', 'New York', 'NY', 11920), 39 | (28, 'Debra Talkington', '8421 Smithfield Ave', 'Mesa', 'AZ', 82573), 40 | (29, 'Peter Johnson', '4213 Jones Pkwy', 'Missoula', 'MS', 37228), 41 | (30, 'Harry Brown', '125 Cinnamon Way', 'Raleigh', 'NC', 27418), 42 | (31, 'Kay Billings', '4592 Jessica Ave', 'Cincinnati', 'OH', 43118), 43 | (32, 'Desmond Rafferty', '6329 Jordan St', 'San Diego', 'CA', 93980), 44 | (33, 'Lisa Washington', '3490 Dearborn Ave', 'Boise', 'ID', 84930), 45 | (34, 'Jackson Taylor', '7733 Miami Way', 'Atlanta', 'GA', 37544), 46 | (35, 'Michael Davis', '9245 Olney Ave', 'Eau Claire', 'WI', 54732), 47 | (36, 'Candice Thompson', '3122 Ebony Blvd', 'Memphis', 'TN', 33152), 48 | (37, 'Terry Mitchell', '7438 Tacoma St', 'Spokane', 'WA', 99753), 49 | (38, 'Ruth Bolen', '5584 Frigid Ln', 'Anchorage', 'AK', 99831), 50 | (39, 'James Hamilton', '449 Routiers Ave', 'Ft Lauderdale', 'FL', 33912), 51 | (40, 'Harvey Akins', '7950 Ridge Rd', 'Newark', 'NJ', 2753), 52 | (41, 'Timothy Smothers', '5892 Maple Ave', 'Washington', 'DC', 213), 53 | (42, 'Stephanie Nichols', '2275 Apple St', 'Highland', 'MI', 49003), 54 | (43, 'Ken Davidson', '1583 N 27th Rd', 'Dallas', 'TX', 78432), 55 | (44, 'Angel Martin', '3195 Mapleton Ave', 'Providence', 'RI', 8132), 56 | (45, 'Cathy Douglas', '8934 Glory St', 'Boulder', 'CO', 89578), 57 | (46, 'Kendall Norton', '8733 Jordan Ave', 'Kansas City', 'KS', 66555), 58 | (47, 'Desire Morton', '2138 Peaches Way', 'Ellettsville', 'IL', 67330), 59 | (48, 'Patty Zrock', '7397 Happy St', 'Brooklyn', 'NY', 13420), 60 | (49, 'Evan Bayh', '6613 North St', 'Miami', 'FL', 33990), 61 | (50, 'Kevin Masters', '91350 Carmel Dr', 'Brisket', 'WY', 80154), 62 | (51, 'Paula Barker', '6528 Prozac Ave', 'San Francisco', 'CA', 92953), 63 | (52, 'John Jacobs', '2957 Ashway Ct', 'Pittsburgh', 'PA', 18320), 64 | (53, 'Dana Brunswick', '4825 Polly Ln', 'Las Vegas', 'NV', 89223), 65 | (54, 'David Turner', '743 Main St', 'Knoxville', 'TN', 37710), 66 | (55, 'Michael Orlando', '1355 Joseph Way', 'Pensacola', 'FL', 31552), 67 | (56, 'Terry Green', '6320 LaSalle St', 'Yazoo', 'MS', 30428), 68 | (57, 'Greg Evans', '2294 South St', 'Indianapolis', 'IN', 46203), 69 | (58, 'Sandy Johnson', '5319 Brentwood Dr', 'Louisville', 'KY', 41350), 70 | (59, 'Charlie Jones', '5382 Miracle Ln', 'New Orleans', 'LA', 73210), 71 | (60, 'Kevin Grier', '855 Henry St', 'Provo', 'UT', 83111), 72 | (61, 'Lani Kulana', '3352 Hibiscus Ln', 'Honolulu', 'HI', 93525), 73 | (62, 'Mary Worley', '4291 Cherry St', 'Oaktown', 'NH', 5308), 74 | (63, 'Tanya Gleason', '7315 Franklin Rd', 'Peoria', 'IL', 66514), 75 | (64, 'Donna Bradley', '2753 Florida Way', 'San Jose', 'CA', 91342), 76 | (65, 'Jimmy Lawson', '9 Corporate Dr', 'Excelsior', 'GA', 37833), 77 | (66, 'Brenda Fowler', '8447 Jackson Pl', 'St Louis', 'MO', 63153), 78 | (67, 'Karen Mills', '9077 Cumberland Way', 'Tulsa', 'OK', 74319), 79 | (68, 'Aretha Gordon', '1489 Pumpkin Ave', 'Appleton', 'WI', 54310), 80 | (69, 'Carol Brown', '6225 Tamiami Trl', 'Ft Myers', 'FL', 33914), 81 | (70, 'Jerry Freeman', '7840 Princess Dr', 'Topeka', 'KS', 63330), 82 | (71, 'LuAnn Dennis', '5602 Norman St', 'Philadelphia', 'PA', 17569), 83 | (72, 'Thomas Jackson', '3958 Breen Ave', 'Mobile', 'AL', 34558), 84 | (73, 'Marilyn Pritchett', '1468 Duncan St', 'Trenton', 'NJ', 7104), 85 | (74, 'Katherine Cain', '4388 Drysdale Ln', 'Texarkana', 'AR', 77315), 86 | (75, 'Patsy Cline', '9926 Hemingway Blvd', 'Tuscaloosa', 'CA', 97221), 87 | (76, 'Kerry Jackson', '5533 Penelope St', 'Beaverton', 'OR', 91353), 88 | (77, 'Princess Wilson', '3127 Spencer Dr', 'Norton', 'sC', 27384), 89 | (78, 'Henry Fisk', '6341 Wilmington Ave', 'Lincoln', 'NE', 70011), 90 | (79, 'Jessica Grant', '2203 Pansy Ln', 'Butte', 'MT', 54438), 91 | (80, 'Joe Franklin', '1078 Dusky Dr', 'Independence', 'IN', 46935), 92 | (81, 'Larry Hood', '3522 Potter St', 'Marietta', 'GA', 37023), 93 | (82, 'Jim OBrien', '6084 Maple St', 'Boston', 'MA', 1358), 94 | (83, 'Tanya Hicks', '1487 Potter St', 'Orlando', 'FL', 34258), 95 | (84, 'Dean Williams', '2279 Gray St', 'Roanoke', 'VA', 27384), 96 | (85, 'Jason Freeman', '4301 Perry Ave', 'Lincoln', 'NE', 67301), 97 | (86, 'Katy Smith', '8733 Johnson Way', 'Santa Fe', 'NM', 80227), 98 | (87, 'Andy Jones', '5177 US 42 S', 'Paragon', 'MI', 48203), 99 | (88, 'Crystal Jarvis', '9028 Indiana Ave', 'Tuscaloosa', 'AL', 32784), 100 | (89, 'Elizabeth McMurray', '730 Walnut St', 'Mooresville', 'WI', 53802), 101 | (90, 'Cassandra Tansy', '1209 Broadway St', 'Bloomington', 'IN', 46801), 102 | (91, 'Dick Moore', '3209 Simpson Rd', 'New York', 'NY', 10220), 103 | (92, 'Marisa Rodriguez', '873 Olney Ave', 'Muncie', 'VA', 20384), 104 | (93, 'Tyrone Jackson', '7725 Georgia St', 'Salt Lake City', 'UT', 82004), 105 | (94, 'Donald Gray', '8339 Olivia St', 'Champaign', 'IL', 60337), 106 | (95, 'Victoria Jameson', '2804 Oak Tree St', 'Salina', 'KS', 67905), 107 | (96, 'Lucy Bronson', '5336 Michigan Ave', 'Wilmington', 'DE', 1903); 108 | 109 | 110 | CREATE TABLE IF NOT EXISTS items( 111 | id int(11) NOT NULL AUTO_INCREMENT, 112 | name varchar(30) NOT NULL, 113 | cost float NOT NULL, 114 | seller_id int(11) NOT NULL, 115 | bids int(11) NOT NULL, 116 | PRIMARY KEY(id) 117 | ) 118 | 119 | INSERT INTO items(id,name,cost,seller_id,bids) VALUES 120 | (1, 'Brand New iMac Computer', 149.99, 32, 3), 121 | (2, 'used diaper from my sister', 2.04, 1, 0), 122 | (3, 'Fresh apple pie', 14.99, 54, 32), 123 | (4, 'New gym socks', 2.34, 90, 566), 124 | (5, 'Weedwacker only slightly used', 4.56, 84, 2), 125 | (6, 'New ipad stolen from best buy', 399, 32, 23), 126 | (7, 'Book about having babies', 21.34, 44, 21), 127 | (8, 'Woman Jeans', 49.5, 56, 123), 128 | (9, 'traditional carpet', 25.45, 14, 75), 129 | (10, '3 boxes of frogs', 30.49, 68, 145), 130 | (11, '48 boxes of frogs', 74.29, 6, 99), 131 | (12, '7 boxes of frogs', 857.75, 18, 88), 132 | (13, 'laptop', 743.3, 89, 158), 133 | (14, 'thumbelina', 228.05, 15, 49), 134 | (15, 'bed', 127.15, 65, 189), 135 | (16, 'shampoing', 12.8, 6, 105), 136 | (17, 'stove', 37.66, 68, 111), 137 | (18, 'cushion', 7.15, 97, 157), 138 | (19, 'refrigerator', 657.49, 61, 129), 139 | (20, 'gold necklace', 853.07, 10, 101), 140 | (21, 'pan', 33.7, 7, 184), 141 | (22, 'awesome alien computer game', 10.75, 18, 29), 142 | (23, 'baby coat', 89.99, 14, 47), 143 | (24, 'baby seat', 145.78, 2, 199), 144 | (25, 'satchel', 44.71, 15, 66), 145 | (26, 'women perfum', 110.9, 48, 84), 146 | (27, 'conveyor belt', 1120.75, 11, 4), 147 | (28, 'used car', 5700.5, 12, 135), 148 | (29, 'supercomputer', 49.75, 50, 176), 149 | (30, 'mirror', 26.8, 19, 56), 150 | (31, 'piano', 1800.4, 13, 147), 151 | (32, 'quitar', 88.4, 25, 164), 152 | (33, 'trumpet', 255.15, 36, 23), 153 | (34, 'machintosh', 3845, 20, 107), 154 | (35, 'earphone', 10.5, 17, 110), 155 | (36, 'computer', 418, 11, 152), 156 | (37, 'night light', 13.87, 97, 198), 157 | (38, 'pc bag', 50.99, 48, 65), 158 | (39, 'babyfoot', 376.7, 2, 121), 159 | (40, 'hairdryer', 88.9, 12, 177), 160 | (41, 'babyliss', 130.75, 68, 79), 161 | (42, 'door', 150.5, 98, 13), 162 | (43, 'baby soap', 12.7, 4, 198), 163 | (44, 'used phone', 43.75, 9, 69), 164 | (45, 'bath', 757.15, 96, 55), 165 | (46, 'flower', 10.75, 16, 89), 166 | (47, 'battery charger', 48.75, 25, 87), 167 | (48, 'air conditioner', 975, 12, 151), 168 | (49, 'casserole', 115.75, 46, 35), 169 | (50, 'used toilet', 180.7, 64, 11), 170 | (51, 'teashirt', 14.98, 65, 114), 171 | (52, 'moto', 920, 22, 174), 172 | (53, 'saxophone', 220.9, 60, 140), 173 | (54, 'bicycle', 180.55, 97, 35), 174 | (55, 'man perfum', 95, 75, 199), 175 | (56, 'table', 157.25, 91, 48), 176 | (57, 'boat', 4890.5, 17, 177), 177 | (58, 'iphone', 547, 8, 28), 178 | (59, 'body milk', 50.5, 16, 90), 179 | (60, 'new curtain for bedroom', 278.4, 92, 11), 180 | (61, 'diamond ring', 1900, 15, 45), 181 | (62, 'swept', 4.5, 9, 99), 182 | (63, 'women hat', 17.55, 39, 60), 183 | (64, 'washing machine', 680.9, 42, 125), 184 | (65, 'baby bottle', 27.98, 91, 117), 185 | (66, 'women sun glasses', 66.7, 18, 174), 186 | (67, 'person weighs', 65.25, 10, 100), 187 | (68, 'photo frame', 18, 85, 170), 188 | (69, 'key board', 16.7, 90, 101), 189 | (70, 'screen', 250, 81, 188), 190 | (71, 'bucket', 2.5, 1, 19), 191 | (72, 'lipstick', 24.75, 3, 44), 192 | (73, 'wardrobe', 120.75, 9, 71), 193 | (74, 'blue dress size 40', 88.9, 7, 113), 194 | (75, 'newspaper', 1.5, 95, 172), 195 | (76, 'scanner', 350, 14, 62), 196 | (77, 'camera', 550.7, 17, 95), 197 | (78, 'camcorder', 788.99, 25, 127), 198 | (79, 'gun', 420.1, 81, 107), 199 | (80, 'domestic dog', 200, 19, 129), 200 | (81, 'horse', 759.5, 30, 115), 201 | (82, 'truck', 7800.5, 32, 123), 202 | (83, 'soccer ball', 95.49, 54, 155), 203 | (84, 'gold earring', 385, 75, 92), 204 | (85, 'basket', 250.45, 46, 142), 205 | (86, 'bikini', 85.2, 12, 57), 206 | (87, 'red skirt', 15.9, 18, 188), 207 | (88, 'copier machine', 800.7, 50, 160), 208 | (89, 'handbag', 35.9, 8, 108), 209 | (90, 'bath towel', 25.1, 11, 186), 210 | (91, 'coffee machine', 210.89, 15, 170), 211 | (92, 'wedding dress', 690, 26, 48), 212 | (93, 'man sun glasses', 80.7, 19, 174), 213 | (94, 'candle', 7.5, 22, 102), 214 | (95, 'scarf', 11.9, 7, 143), 215 | (96, 'microwave', 150.29, 6, 11), 216 | (97, 'electric oven', 645, 62, 171), 217 | (98, 'play station', 256.75, 12, 188), 218 | (99, 'dvd', 126.84, 14, 113), 219 | (100, 'magazine', 3.5, 8, 152); 220 | -------------------------------------------------------------------------------- /to_char & Extract.sql: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT (TO_CHAR(payment_date, 'MONTH')) 2 | FROM payment; 3 | 4 | SELECT COUNT(*) FROM payment 5 | WHERE EXTRACT(dow FROM payment_date) = 1; 6 | --------------------------------------------------------------------------------