You are on page 1of 7

Data definition language (DDL): it is used to create a table or aleter a table

CREATE, ALTER, DROP, etc.


Data manipulation language (DML): It is used to manipulate existing data in the
database. SELECT, UPDATE, INSERT, DELETE etc.
Data control language (DCL): GRANT and REVOKE.
Transaction Control Language (TCL): I COMMIT, ROLLBACK, SET TRANSACTION, SAVEPOINT,
etc.

SELECT * FROM Table_A JOIN Table_B;

SELECT * FROM Table_A INNER JOIN Table_B;

SELECT * FROM Table_A A LEFT JOIN Table_B B ON A.col = B.col;

SELECT FROM myDb.students WHERE student_id = 1;


UPDATE myDB.students SET fname = 'Captain', lname = 'America' WHERE student_id = 1

1. here's a SQL query to find the highest salary from a table:

SELECT MAX (salary) AS highest_salary FROM empolye;

2. To delete duplicate rows from a table in SQL

WITH CTE AS (SELECT *,ROW_NUMBER() OVER (PARTITION BY column1, column2, ... ORDER
BY (SELECT NULL)) AS RowNumber
FROM empolye
)
DELETE FROM CTE
WHERE RowNumber > 1;

here are some examples of SQL join queries:

INNER JOIN: Retrieves records that have matching values in both tables.

SELECT t1.column1, t2.column2


FROM table1 t1
INNER JOIN table2 t2 ON t1.common_column = t2.common_column;

LEFT JOIN (or LEFT OUTER JOIN): Retrieves all records from the left table and the
matched records from the right table

SELECT t1.column1, t2.column2


FROM table1 t1
LEFT JOIN table2 t2 ON t1.common_column = t2.common_column;

RIGHT JOIN (or RIGHT OUTER JOIN): Retrieves all records from the right table and
the matched records from the left table.

SELECT t1.column1, t2.column2


FROM table1 t1
RIGHT JOIN table2 t2 ON t1.common_column = t2.common_column;

FULL JOIN (or FULL OUTER JOIN): Retrieves all records when there is a match in
either left or right table.
SELF JOIN: Joining a table with itself.

Retrieve all columns from a table:SELECT * FROM empolye;


Retrieve specific columns from a table: SELECT column1, column2 FROM empolye;
Filter rows based on a condition: SELECT * FROM empolye WHERE condition;
Count the number of rows in a table: SELECT COUNT(*) FROM empolye;
Find the maximum value in a column: SELECT MAX(column_name) FROM empolye;
Find the minimum value in a column: SELECT MIN(column_name) FROM empolye;
Find the average value in a column: SELECT AVG(column_name) FROM empolye;

Group rows based on a column and apply aggregate functions:

SELECT column1, COUNT(*)FROM empolye


GROUP BY column1;
Join two tables:

SELECT t1.column1, t2.column2


FROM table1 t1
INNER JOIN table2 t2 ON t1.common_column = t2.common_column;
Delete duplicate rows from a table:

SELECT employee_id, name, salary FROM employee WHERE name = 'Sridhar';


=========================
SELECT * FROM employee WHERE name LIKE 's%';

This query uses the LIKE operator with the % wildcard, which matches any sequence
of characters. So, 's%' matches any value that starts with 's'. Adjust the column
name and table name according to your database schema

==================================================

UPDATE employee
SET salary = 5000
WHERE name = 'Sridhar';

=======================================

what are the tables and fileds in sql

In SQL, tables are the fundamental structure for storing data in a relational
database. Tables consist of rows and columns, where each row represents a record or
tuple, and each column represents a specific attribute or field. Fields are also
commonly referred to as columns.

Here's a brief explanation of tables and fields in SQL:

Tables:

Tables are organized collections of data stored in a database.


Each table has a unique name and consists of one or more columns and zero or more
rows.
Tables are defined with a schema, which specifies the name and data type of each
column.
Fields (or Columns):

Fields, also known as columns or attributes, define the structure of the data
stored in a table.
Each field has a name and a data type that determines the type of data it can store
(e.g.,
===============================
Constraints in SQL are rules that are enforced on columns or tables to ensure data
integrity, accuracy, and consistency. Constraints define the permissible values or
conditions that data in a column or table must satisfy. Here are some common
constraints in SQL:

Primary Key Constraint:

A primary key constraint uniquely identifies each record in a table.


It ensures that the values in the specified column(s) are unique and not null.
Each table can have only one primary key constraint.
Foreign Key Constraint:

A foreign key constraint establishes a relationship between two tables.


It ensures that the values in the specified column(s) of one table match the values
of the primary key column(s) in another table.
It helps enforce referential integrity between related tables.
Unique Constraint:

A unique constraint ensures that all values in the specified column(s) are unique.
Unlike primary key constraints, unique constraints allow null values.
Check Constraint:

A check constraint specifies a condition that must be true for each row in the
table.
It ensures that values entered into a column meet specific criteria.
For example, you can use a check constraint to ensure that the age column only
accepts values greater than zero.
Not Null Constraint:

A not null constraint ensures that a column cannot contain null values.
It requires that every row in the table must have a value for the specified column.
Default Constraint:

A default constraint specifies a default value for a column when no value is


explicitly provided during an insert operation.
It ensures that a default value is used if one is not provided.

=====================

what is primary key in sql

It ensures that each row in the table has a unique value or combination of values
in the specified column(s). The primary key constraint also enforces the column(s)
to not contain null values.

=====================

The main difference between SQL and MySQL lies in their scope and purpose:

SQL (Structured Query Language):

SQL is a standardized programming language used for managing and manipulating


relational databases.
It provides a set of commands for querying data, defining schema, managing
transactions, and performing various operations on databases and their objects
(tables, views, indexes, etc.).
SQL is not a specific database management system (DBMS) but rather a language
standard that is supported by many relational database systems.
MySQL:

MySQL is one of the most popular open-source relational database management systems
(RDBMS) that implements the SQL language.
It is developed, distributed, and supported by Oracle Corporation.
MySQL provides a full-featured database server that supports SQL queries,
transactions, stored procedures, triggers, and other advanced features typical of
modern relational database systems.
MySQL uses SQL as its query language, adhering to the SQL standard while also
adding some proprietary extensions and optimizations.

==================================

The GROUP BY clause in SQL is used to group rows that have the same values in
specified columns into summary rows. It is typically used in conjunction with
aggregate functions (such as SUM, AVG, COUNT, MAX, MIN) to perform calculations on
grouped data. Here are some common scenarios when you would use the GROUP BY
clause:

========================

what is index in sql and why we use

In SQL, an index is a database object used to improve the speed of data retrieval
operations on tables. Indexes are created on one or more columns of a table, and
they provide a quick access path to the data based on the values stored in those
columns. Here's why indexes are used and how they work:

Improving Query Performance:


Supporting Joins
Efficient Data Retrieval:

==========================
normalization

It involves breaking down a large table into smaller, more manageable tables and
defining relationships between them. Normalization aims to improve data integrity,
reduce data redundancy, and facilitate efficient data manipulation.

============================
keys concept in sql

Primary Key (PK):

A primary key is a column or combination of columns that uniquely identifies each


row in a table.
Primary keys enforce entity integrity, ensuring that each row has a unique
identifier and cannot contain null values.
Each table can have only one primary key, and it is typically defined using the
PRIMARY KEY constraint.
Foreign Key (FK):

A foreign key is a column or combination of columns in one table that refers to the
primary key or unique key in another table.
Foreign keys establish relationships between tables, enforcing referential
integrity by ensuring that values in the foreign key column(s) match values in the
referenced primary key column(s) or unique key column(s) in the related table.
Foreign keys are defined using the FOREIGN KEY constraint.
Unique Key (UK):

A unique key is a column or combination of columns that ensures uniqueness but does
not necessarily serve as the primary key.
Unique keys enforce uniqueness constraints on the values in the specified
column(s), allowing null values.
Unlike primary keys, tables can have multiple unique keys.
Unique keys are defined using the UNIQUE constraint.
Alternate Key:

An alternate key is a candidate key that is not selected as the primary key.
It can be used as a unique identifier for rows but is not designated as the primary
key.
Alternate keys can be defined using unique constraints.
Composite Key:

A composite key is a key that consists of multiple columns, used together to


uniquely identify rows in a table.
Composite keys are commonly used when no single column can uniquely identify rows,
but a combination of columns can.
Composite keys are defined by combining multiple columns in the PRIMARY KEY or
UNIQUE constraint.
=======

operator

LIKE
IN OPERATOR
BETWEEN OPERATOR
AND OR NOT IS OPERATOR

LIKE Operator:

The LIKE operator is used in SQL to search for a specified pattern in a column.
It is commonly used with the % wildcard symbol to match any sequence of characters
and the _ wildcard symbol to match any single character.
Example:
sql
Copy code
SELECT * FROM employees WHERE last_name LIKE 'Sm%';
This query selects all employees whose last names start with 'Sm'.
IN Operator:

The IN operator is used to specify multiple values in a WHERE clause to be tested


against a column.
It allows you to simplify complex queries by avoiding multiple OR conditions.
Example:
sql
Copy code
SELECT * FROM products WHERE category_id IN (1, 2, 3);
This query selects all products that belong to category IDs 1, 2, or 3.
BETWEEN Operator:

The BETWEEN operator is used to filter results based on a range of values.


It is inclusive, meaning that it includes both the start and end values specified
in the range.
Example:
sql
Copy code
SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-01-31';
This query selects all orders placed between January 1st, 2022 and January 31st,
2022.
AND, OR, NOT Operators:

The AND operator is used to combine multiple conditions in a WHERE clause. It


requires all conditions to be true for the row to be included in the result set.
The OR operator is used to combine multiple conditions in a WHERE clause. It
requires at least one condition to be true for the row to be included in the result
set.
The NOT operator is used to negate a condition in a WHERE clause, meaning it
returns true if the condition is false and vice versa.
Example:
sql
Copy code
SELECT * FROM customers WHERE city = 'New York' AND state = 'NY';
This query selects all customers from New York City in the state of New York.
IS Operator:

The IS operator is used to compare a value to NULL.


It is used instead of the equality operator (=) when comparing to NULL because NULL
is not equal to any value, including NULL itself.
Example:
sql
Copy code
SELECT * FROM products WHERE price IS NULL;

==========================================================

ORDER BY

GROUP BY
HAVING

ORDER BY Clause:

The ORDER BY clause is used to sort the result set of a SELECT query based on one
or more columns.
It allows you to specify the sort order (ascending or descending) for each column.
Example:
sql
Copy code
SELECT * FROM employees ORDER BY last_name ASC, first_name ASC;
This query selects all employees and orders the result set by last name in
ascending order, then by first name in ascending order.
GROUP BY Clause:

The GROUP BY clause is used to group rows that have the same values in specified
columns into summary rows.
It is typically used in conjunction with aggregate functions (such as SUM, AVG,
COUNT, MAX, MIN) to perform calculations on grouped data.
Example:
sql
Copy code
SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department;
This query groups employees by department and calculates the count of employees in
each department.
HAVING Clause:

The HAVING clause is used to filter the result set produced by the GROUP BY clause.
It is similar to the WHERE clause but is used specifically with aggregate functions
to filter groups based on their calculated values.
Example:
sql
Copy code
SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department
HAVING COUNT(*) > 5;
This query groups employees by department and calculates the count of employees in
each department, but only includes departments with more than 5 employees in the
result.

===================================================

CASE WHEN
EXISTS

ANY
ALL
NULL VALUES

You might also like