SQL Flashcards

(55 cards)

1
Q

What Is SQL

A

Structured Query Language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is rational database? State along with examples

A

The databases in which data is stored in tables. For Example MySQL, Oracle, PostgreSQL etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is non-relational database? State along with examples

A

The databases in which data is not stored in tables. For Example Mongo DB.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What kind of functions does SQL perform?

A

CRUD
Create
Read
Update
Delete

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is DBMS?

A

Database Management System

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

State different data types primarily uses in SQL

A
  1. Integer Data
  2. Character (Text)
  3. Monetary
  4. Date and Time
  5. Binary Strings
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

State function used for integer data

A

INT(number) - Round down to nearest integer
TRUNC(number) - Cut off decimal, no rounding

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

State function used for monetary data

A

DOLLAR(number, decimals) - Formats
number as currency (e.g.,
DOLLAR(1234.56, 2) = “$1 ,234.56”)

TEXT(number, “$#,##0.00”)- Custom currency formatting using symbols and
separators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

State function used for Character (Text)

A

CHAR(number) - Returns character for
an ASCII code (e.g., CHAR(65) = “A”)

TEXT(value, format) - Converts
numbers/dates to formatted text (e.g.,
TEXT(90, “0%”) = “90%”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which functions are used for binary strings

A

DEC2BlN(number) - Converts decimal to
binary (e.g., DEC2BlN(5) = “101”)

BIN2DEC(binary) - Converts binary to
decimal (e.g., BIN2DEC(“101”)= 5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

State functions used for date and time data

A

DATE(year, month, day) – Combines numbers into a date (e.g., DATE(2025, 4, 23))
TIME(hour, min, sec) – Combines time parts into a time value
NOW() / TODAY() – Current date & time / date only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Query to create a database

A

CREATE DATABASE db name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Query to drop a database

A

DROP DATABASE db name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Query to use a particular database

A

USE db_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

List different types of data constraints in sql

A
  1. NOT NULL Constraint.
  2. UNIQUE Constraint.
  3. DEFAULT Constraint.
  4. CHECK Constraint.
  5. PRIMARY KEY Constraint.
  6. FOREIGN KEY Constraint.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to use UNIQUE constraint and what does it do?

A

Ensures all values in a column are unique. No duplicates allowed.
Example: email VARCHAR(100) UNIQUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How to use PRIMARY KEY constraint and what does it do?

A

Uniquely identifies each row in a table.
Combines NOT NULL + UNIQUE.
Example: id INT PRIMARY KEY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to use NOT NULL constraint and what does it do?

A

Ensures the column cannot have NULL values.
Every row must contain a value.

Example: name VARCHAR(100) NOT NULL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How to use FOREIGN KEY constraint and what does it do?

A

Links a column to the primary key of another table.
Enforces referential integrity.
Example: student_id INT REFERENCES students(id)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How to use CHECK constraint and what does it do?

A

Ensures all values in a column meet a specific condition.
Example: age INT CHECK (age 18)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Query to write a table

A

USE db name;
CREATE TABLE table_name (
col_name1 datatype constraint,
col_name2 datatype constraint,
col_name3 datatype constraint
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How to insert values into a table

A

INSERT INTO db_name.table_name
(col1_name,col2_name)
VALUES
(1,”data1”),
(2,”data2”);

For Example
INSERT INTO first.table1
(id,name)
VALUES
(1,”Adam”),
(2,”Eve”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How to print all values of a table?

A

SELECT*FROM table_name;

24
Q

How to print selected value of a table?

A

SELECT col1,col2 FROM table_name:

25
How to print distinct values of a table?
SELECT DISTINCT FROM table_name;
26
What does WHERE clause do?
WHERE clause prints specific values of a table which satisfies a particular condition with the help of comparison / logical / arithmetic / bitwise operators E.g. SELECT * FROM table_name where id > 2 ;
27
What does LIMIT clause do?
LIMIT clause sets an upper limit on number of (tuples)rows to be returned E.g. SELECT * FROM table_name LIMIT number;
28
What does ORDER clause do?
It arranges values of a table in an ascending / descending order e.g. SELECT*FROM table_name ORDER BY col_names(s) ASC/DESC;
29
What are aggregate functions? List different aggregate functions.
Aggregate functions perform a calculation on a set of values, and return a single value. 1. COUNT( ) 2. MAX() 3. MIN() 4. SUM() 5. AVG()
30
What does GROUP BY clause do?
Groups rows that have the same values into summary rows. It collects data from multiple records and groups the result by one or more column. For Example, SELECT city, count (name) FROM student GROUP BY city;
31
What does HAVING clause do?
Similar to Where i.e. applies some condition on rows. Used when we want to apply any condition after grouping. For Example, SELECT count (name), city FROM student GROUP BY city HAVING max(marks) > 90;
32
What is the general order to write different clauses in a single query?
SELECT column(s) FROM table_name WHERE condition GROUP BY column(s) HAVING condition ORDER BY column(s) ASC/DESC ;
33
How do you update existing rows in a table?
UPDATE table_name SET col1 = val1, col2 = val1 WHERE condition; For Example, UPDATE my table SET status = "Allowed" WHERE age >= 18;
34
How to turn on Safe Mode in MySQL?
SET SQL_SAFE_UPDATES = 1;
35
How to turn on Safe Mode in MySQL?
SET SQL_SAFE_UPDATES = 0;
36
How to delete existing rows in a table?
DELETE FROM table_name WHERE condition; For Example, DELETE FROM userdata WHERE age<18;
37
What is an EER diagram?
Enhanced Entity Relationship Diagram It helps you visualize how different tables are connected and related in your database.
38
What is the syntax for using FOREIGN KEY
Will use an example here to get the grasp of it in a better way, CREATE TABLE country ( code INT PRIMARY KEY, country_name VARCHAR(100) ); CREATE TABLE userdata ( user_id INT PRIMARY KEY, user_name VARCHAR(100) user_country INT FOREIGN KEY (user_country) REFERENCES country (code) );
39
Define parent and child table with the help of an example.
A parent is the table that stores the primary key, A child is any table that references the parent with a foreign key. For Example, Parent table: Students – holds student info. Child table: ExamScores – holds exam results linked to students. One student → many exam scores.
40
What is ON DELETE CASCADE?
When we create a foreign key using this option, it deletes the referencing rows in the child table when the referenced row is deleted in the parent table which has a primary key.
41
What is ON UPDATE CASCADE?
When we create a foreign key using UPDATE CASCADE the referencing rows are updated in the child table when the referenced row is updated in the parent table which has a primary key.
42
How to add a column in a table?
ALTER TABLE table_name ADD COLUMN column_name datatype_constraint;
43
How to delete a column in a table?
ALTER TABLE table_name DROP COLUMN column_name datatype_constraint;
44
How to rename a existing table?
ALTER TABLE table_name RENAME TO new_table_name;
45
How to rename an existing column?
ALTER TABLE table_name CHANGE COLUMN old_ name new_name new_datatype new_constraint;
46
How to modify datatype/constraint of a column?
ALTER TABLE table_name MODIFY COL name new-datatype new-constraint;
47
How to delete a table's data?
TRUNCATE table table_name;
48
What is joins in SQL?
Joins is used to combine rows from two or more tables based on a related column between them.
49
List different types of join.
1. Inner Join 2. Left Join 3. Right Join 4. Full Join
50
How does inner join work?
For Example there are two tables Table A and B The data overlapping in both Table A and B will consist of inner join. SELECT column(s) FROM tableA INNER JOIN tableB ON tableA.col_name = tableB.col_name;
51
How does left join work?
For Example there are two tables Table A and B Here, table A is left and B is right Now the left join will consist of Data overlapping between A and B and data which only exists in table A. SELECT column(s) FROM tableA LEFT JOIN tableB ON tableA.col_name = tableB.col_name;
52
How does right join work?
For Example there are two tables Table A and B Here, table A is left and B is right Now the Right join will consist of Data overlapping between A and B and data which only exists in table B. SELECT column(s) FROM tableA RIGHT JOIN tableB ON tableA.col_name = tableB.col_name;
53
How does full join work?
For Example there are two tables Table A and B Full join will consist of data existing in Table A and B both. SELECT * FROM student LEFT JOIN course ON student. id = course. id UNION SELECT * FROM student RIGHT JOIN course ON student. id = course. id;
54
What is left exclusive join?
For Example there are two tables Table A and B Here, table A is left and B is right Left exclusive will only include values from table A excluding values overlapping between table A and B. For Example, SELECT * FROM tableA LEFT JOIN tableb ON tableA.col = tableB.col WHERE tableB.col IS NULL;
55
What is right exclusive join?
For Example there are two tables Table A and B Here, table A is left and B is right Right exclusive will only include values from table B excluding values overlapping between table A and B. For Example, SELECT * FROM tableA RIGHT JOIN tableb ON tableA.col = tableB.col WHERE tableA.col IS NULL;