Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

SQL Queries Class 12

The document provides a comprehensive overview of SQL queries and commands, including database creation, table manipulation, and various types of joins. It also includes questions and multiple-choice questions related to database concepts, integrity constraints, and SQL functions. Additionally, it covers practical SQL statements for data retrieval and manipulation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SQL Queries Class 12

The document provides a comprehensive overview of SQL queries and commands, including database creation, table manipulation, and various types of joins. It also includes questions and multiple-choice questions related to database concepts, integrity constraints, and SQL functions. Additionally, it covers practical SQL statements for data retrieval and manipulation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SQL QUERIES

1) Create database

2) Show databases

3) Use database

4) Create Table

5) Describe table

6) Add column in table

7) Make primary key


8) Add values in table

9) Show table

10) Show particular column

Multiple columns
can also be
selected

11) Show the information of any particular on the basis of given information

12) Update any information based on given information

13) Change type of any information


14) Delete any column

15) Delete any particular section

16) Sum , avg , min , max of any column of table

17) Show total no. of information present for a field

18) Show table ordered by ascending or descending for any section

I added few more data in the table

19) To match the names whose


a) First letter is ‘A’

b) Last letter is ‘A’

c) Containing ‘a’ anywhere

d) Second character is ‘a’


e) third character is ‘a’

f) whose second last alphabet is ‘a’

Created a second table name employees

#TYPES OF JOINS

1) INNER JOIN:

This inner join will match rows from both info and employees
tables where the roll_no in the info table matches the id in the
employees table. (fields may vary in this case which is roll no. and id)

2) RIGHT JOIN

This right join will return all rows from the employees table, along
with matching rows from the info table based on the condition.
If there are no matches in the info table, it will return NULL
values for the columns of the info table.

3) LEFT JOIN :

This left join will return all rows from the info table, along with
matching rows from the employees table based on the
condition. If there are no matches in the employees table, it
will return NULL values for the columns of the employees
table.

4) CROSS JOIN:
This cross join will return the Cartesian product of both tables,
resulting in all possible combinations of rows from the info table
with rows from the employees table. It does not require any
condition to be specified.

OUTPUT:

QUESTIONS
Q1. State two advantages of using Databases.

Q2. Name some popular relational database management systems.

Q3. Define – Relation, Tuple, Degree, Cardinality.

Q4. What is Data Dictionary?

Q5. Name some data types in MySQL.

Q6. Differentiate between Char and Varchar.

Q7. What is a Primary Key?

Q8. What is a Foreign Key? What is its use?

Q9. Write SQL statements to do the following:


(a) Create a table Result with two columns Roll and Name with Roll as
primary key.
(b) Add a column Marks to Result table.
(c) Insert a record with values 1, “Raj”, 75.5.
(d) Show the structure of Result table.
(e) Display the records in ascending order of name.
(f) Display records having marks>70.
(g) Update marks of "Raj" to 80.

Q10. What are the various Integrity Constraints?

Q11. What is ORDER BY CLAUSE in SQL?

Q12. What is Aggregate function in MYSQL?

Q13. What is group by clause in MYSQL?

Q14. Consider the following tables DRESS and MATERIAL. Write SQL
commands for the statements

(i) To display DCODE and DESCRIPTION of each dress in ascending order of DCODE.
(ii) To display the details of all the dresses which have LAUNCHDATE in between 05–DEC–
07 and 20–JUN–08 (inclusive of both the dates).
(iii) To display the average PRICE of all the dresses which are made up of material with
MCODE as M003.
(IV) To display material wise highest and lowest price of dresses from DRESS table. (Display
MCODE of each dress along with highest and lowest price)
(v) SELECT SUM (PRICE) FROM DRESS WHERE MCODE=‘M001’;
(vi) SELECT DESCRIPTION, TYPE FROM DRESS, MATERIAL WHERE DRESS.
DCODE=MATERIAL. MCODE AND DRESS. PRICE>=1250.
(vii) SELECT MAX(MCODE) FROM MATERIAL.
(viii) SELECT COUNT(DISTINCT PRICE) FROM DRESS

15) What is the difference between WHERE and HAVING clauses?

MCQ

1. Which of the following is an advantage of using databases over traditional


file systems?
a) Increased data redundancy b) Reduced data inconsistency
c) Limited data security d) Lack of data integrity

2. Which of the following is not a popular relational database management


system?
a) Oracle b) MongoDB c) MySQL d) SQLite

3. What does the term "Tuple" refer to in the context of databases?


a) A row in a relation b) A column in a relation c) A unique identifier
for a relation
d) A constraint applied to a relation

4. Which of the following statements is true about a Primary Key?


a) It can contain NULL values b) It can be duplicated in a table
c) It uniquely identifies each record in a table
d) It is not a constraint in database design

5. What is the purpose of a Foreign Key in a database table?


a) It uniquely identifies each record in a table
b) It establishes a link between two tables
c) It enforces uniqueness of values in a column
d) It defines the structure of a table

6. Which SQL statement is used to create a new database?


a) CREATE TABLE b) USE DATABASE c) CREATE DATABASE d)
ALTER TABLE

7. Which SQL clause is used to add a new column to an existing table?


a) ADD COLUMN b) INSERT INTO c) ALTER TABLE d)
UPDATE TABLE

8. What is the purpose of the ORDER BY clause in SQL?


a) To filter records based on a condition b) To join multiple tables
c) To sort the results of a query d) To perform aggregate functions on
data

9. Which SQL function is used to calculate the sum of values in a column?


a) AVG() b) MAX() c) SUM() d) COUNT()

10. What does the SQL query `SELECT * FROM table WHERE column LIKE
'A%'` do?
a) Selects all records where the column starts with 'A'
b) Selects all records where the column contains 'A'
c) Selects all records where the column ends with 'A'
d) Selects all records where the column exactly matches 'A'

11. Which SQL statement is used to display the structure of a table?


a) DESCRIBE TABLE b) SHOW TABLE c) DISPLAY TABLE d)
DESCRIBE

12. What does the SQL function AVG() do?


a) Returns the maximum value in a column
b) Returns the minimum value in a column
c) Returns the average value of a column
d) Returns the count of values in a column

13. Which of the following integrity constraints ensures that all values in a
column are distinct?
a) UNIQUE b) CHECK c) PRIMARY KEY d) FOREIGN
KEY

14. In a relational database, what does the term "Cardinality" refer to?
a) The number of records in a table b) The number of columns in a
table
c) The number of distinct values in a column
d) The number of unique combinations of values in a table

15. What is the purpose of the GROUP BY clause in SQL?


a) To filter records based on a condition
b) To sort the results of a query
c) To group rows that have the same values into summary rows
d) To perform aggregate functions on data

16. Which SQL statement is used to delete a column from an existing table?
a) DELETE COLUMN b) DROP COLUMN c) REMOVE COLUMN d)
ALTER TABLE

17. What is the purpose of the WHERE clause in SQL?


a) To specify the columns to be retrieved
b) To specify the order of the retrieved rows
c) To filter rows based on a condition
d) To perform calculations on data

18. Which SQL function is used to find the minimum value in a column?
a) MIN() b) MAX() c) SUM() d) COUNT()
19. What is the output of the SQL query `SELECT COUNT(*) FROM table`?
a) The total number of records in the table
b) The average value of all records in the table
c) The sum of all values in the table
d) The number of distinct values in the table

20. Which SQL statement is used to update existing records in a table?


a) MODIFY b) UPDATE c) CHANGE d) SET

21. In SQL, which of the following statements correctly retrieves the average
value of a column named "sales" from a table named "transactions" only for
the year 2023?
a) SELECT AVG(sales) FROM transactions WHERE YEAR(date) = 2023;
b) SELECT AVG(sales) FROM transactions GROUP BY YEAR(date)
HAVING YEAR(date) = 2023;
c) SELECT AVG(sales) FROM transactions WHERE date BETWEEN '2023-
01-01' AND '2023-12-31';
d) SELECT AVG(sales) FROM transactions WHERE date LIKE '%2023%';

22. What does the SQL statement `SELECT DISTINCT column1, column2
FROM table;` do?
a) Selects all unique combinations of values from column1 and column2 in
the table
b) Selects only the distinct values from column1 and column2 in the table
c) Selects all records from the table, displaying only distinct values in
column1 and column2
d) Selects only the distinct values from either column1 or column2 in the
table

23. Which of the following statements about the SQL GROUP BY clause is
correct?
a) The GROUP BY clause can only be used with aggregate functions.
b) The GROUP BY clause sorts the results in ascending order by default.
c) The columns listed in the GROUP BY clause must also appear in the
SELECT clause.
d) The GROUP BY clause can be used to filter rows based on a condition.

24. In SQL, what does the expression `SELECT * FROM table1 INNER JOIN
table2 ON table1.column = table2.column;` accomplish?
a) Retrieves all records from table1 and table2 where the values in the
specified columns match.
b) Retrieves all records from table1 and table2, combining them into a single
result set.
c) Retrieves only the records from table1 that have matching records in
table2 based on the specified column.
d) Retrieves all records from table1 and table2, including non-matching
records, and displays them side by side.

25. What does the SQL statement `SELECT TOP 5 * FROM table ORDER BY
column DESC;` do?
a) Retrieves the top 5 records from the table and orders them by the
specified column in descending order.
b) Retrieves all records from the table and displays only the top 5 unique
values for the specified column.
c) Retrieves the bottom 5 records from the table and orders them by the
specified column in descending order.
d) Retrieves the top 5 records from the table and orders them by the
specified column in ascending order.

You might also like