What is SQL?
On the other hand, SQL
(Structured Query Language
) is the actual language used to perform operations on databases. These operations include the act of inserting data, updating it, deleting it, and so on.
SQL
is a global standard for relational databases. Relational
databases are ones that contain co-related, associative data.
To perform any operation on a table, we write what's called a query
and execute it. A query is a set of instructions written in SQL
. Like any other language, SQL has its own syntax and keywords.
Using SQL
is the most efficient way to process data in datastore. You don’t have to write lengthy code just to perform simple operations. It just takes a query
. The basic queries like inserting, deleting, and updating take no more than a single line of code. Many consider SQL
to be an intuitive language, and almost every database management system (DBMS) supports it.
With this basic understanding of databases and SQL
, let's move on to interview questions based on these concepts.
xxxxxxxxxx
join table t ON r.RandomKey = t.UniqueKey
-- THE BELOW IS SAMPLE SQL
create table RandomKeys (RandomKey int)
create table RandomKeysAttempt (RandomKey int)
-- generate m random keys between 1 and n
declare @cnt int = 0;
while @cnt < m
begin
insert RandomKeysAttempt select rand()*n + 1
end;
-- eliminate duplicates
insert RandomKeys select distinct RandomKey from RandomKeysAttempt
-- as long as we don't have enough, keep generating new keys,
-- with luck (and m much less than n), this won't be necessary
declare @NextAttempt = rand()*n + 1;
while count(RandomKeys) < m
begin
if not exists (select * from RandomKeys where RandomKey = NextAttempt)
begin
insert RandomKeys select NextAttempt
end;
end;
-- get our random rows
select *
from RandomKeys r