SQL Flashcards
(55 cards)
What Is SQL
Structured Query Language
What is rational database? State along with examples
The databases in which data is stored in tables. For Example MySQL, Oracle, PostgreSQL etc.
What is non-relational database? State along with examples
The databases in which data is not stored in tables. For Example Mongo DB.
What kind of functions does SQL perform?
CRUD
Create
Read
Update
Delete
What is DBMS?
Database Management System
State different data types primarily uses in SQL
- Integer Data
- Character (Text)
- Monetary
- Date and Time
- Binary Strings
State function used for integer data
INT(number) - Round down to nearest integer
TRUNC(number) - Cut off decimal, no rounding
State function used for monetary data
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
State function used for Character (Text)
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%”)
Which functions are used for binary strings
DEC2BlN(number) - Converts decimal to
binary (e.g., DEC2BlN(5) = “101”)
BIN2DEC(binary) - Converts binary to
decimal (e.g., BIN2DEC(“101”)= 5)
State functions used for date and time data
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
Query to create a database
CREATE DATABASE db name;
Query to drop a database
DROP DATABASE db name;
Query to use a particular database
USE db_name;
List different types of data constraints in sql
- NOT NULL Constraint.
- UNIQUE Constraint.
- DEFAULT Constraint.
- CHECK Constraint.
- PRIMARY KEY Constraint.
- FOREIGN KEY Constraint.
How to use UNIQUE constraint and what does it do?
Ensures all values in a column are unique. No duplicates allowed.
Example: email VARCHAR(100) UNIQUE
How to use PRIMARY KEY constraint and what does it do?
Uniquely identifies each row in a table.
Combines NOT NULL + UNIQUE.
Example: id INT PRIMARY KEY
How to use NOT NULL constraint and what does it do?
Ensures the column cannot have NULL values.
Every row must contain a value.
Example: name VARCHAR(100) NOT NULL
How to use FOREIGN KEY constraint and what does it do?
Links a column to the primary key of another table.
Enforces referential integrity.
Example: student_id INT REFERENCES students(id)
How to use CHECK constraint and what does it do?
Ensures all values in a column meet a specific condition.
Example: age INT CHECK (age 18)
Query to write a table
USE db name;
CREATE TABLE table_name (
col_name1 datatype constraint,
col_name2 datatype constraint,
col_name3 datatype constraint
);
How to insert values into a table
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 to print all values of a table?
SELECT*FROM table_name;
How to print selected value of a table?
SELECT col1,col2 FROM table_name: