Most voted "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
328
votes4
answers233186
viewsWhat is the difference between INNER JOIN and OUTER JOIN?
What’s the difference between INNER JOIN and OUTER JOIN? Can you give me some examples?
-
199
votes9
answers12451
viewsHow to prevent SQL code injection into my PHP code?
I developed a PHP page for internal use of the company I work with and only very few people use it. Through this page it is possible to make some queries, insertions, changes and removals of data…
-
66
votes7
answers11287
viewsIs it wrong to write byte of images into the database?
When should I make this recording directly at the bank? What situations? I know I can record in the bank the image path.
-
65
votes6
answers66144
viewsType of the CPF or CNPJ field in the VARCHAR or INT database?
What type of CPF or CNPJ field in the database VARCHAR or INT? I’ve seen some posts suggesting using INT to optimize performance in the event of JOINand filters. If you have 0 the left is only…
-
63
votes2
answers17606
viewsWhy use WHERE 1 = 1 in an SQL query?
During the maintenance of a legacy system I found the following Procedure: DECLARE @sql AS varchar(MAX); DECLARE @param as varchar(50); SET @sql = 'SELECT * FROM Destinatario where 1 = 1'; IF(@param…
-
60
votes8
answers16426
viewsWhy is using "SELECT * FROM table" bad?
It is often said to take all columns of a table through the command SELECT * FROM tabela is a bad practice. Any recommendation without explanation is not helpful. So... If it is bad practice, there…
-
50
votes4
answers82876
viewsNot IN or Not EXISTS which to use?
I’ve seen some answers here with the use Not In and some with the use of Not Exists to answer the same question. I don’t know the difference between the two and would like to know about: Which of…
-
50
votes5
answers60507
viewsDISTINCT and GROUP BY, what is the difference between the two statements?
DISTINCT The SELECT DISTINCT statement is used to return only values distinct (different). Within a table, a column usually contains many values duplicated; and sometimes you just want to list the…
-
47
votes1
answer5885
viewsHow to choose between Nosql and SQL?
Nosql databases are there, and one question I always have when starting a project is what criteria to choose between a relational database or not. How to evaluate my project to know the best option…
-
41
votes4
answers4635
viewsTo what extent is it not advisable to use an ORM?
I am working on a very large system using . NET (ASP.NET MVC) the application requires a critical level of performance. How worthwhile or not to use a ORM? Is there a tool that I can compare in…
sql entity-framework software-engineering nhibernate ormasked 10 years, 7 months ago Tuyoshi Vinicius 4,046 -
41
votes2
answers33649
viewsWhat are SQL views? What are the advantages and disadvantages of using?
From my research I learned that views in SQL, are like virtual tables results of stored searches of frequent access. To W3S defines essentially as: virtual table. It has to be said that views are…
-
39
votes3
answers859
viewsI am suffering attacks of type SQL Injection
Ever since I started an online project, I’ve been having problems with hacking, where someone is making direct entries into the database. This is what gave me the initiative to put in all the…
-
39
votes1
answer20752
viewsIs it possible to do an UPDATE with data from another table?
I know it’s possible to execute one INSERT with data from another table: INSERT INTO Tabela (Col01, Col02, Col03) SELECT Col01, Col02, Col03 FROM Outra_tabela WHERE Condicao = 'qualquer coisa' But…
-
38
votes3
answers1203
viewsWhy do parameterized SQL queries (name = ?) prevent SQL Injection?
Why parameterized SQL queries(nome = ?) previnem SQL Injection? Can cite examples?
-
34
votes3
answers8960
viewsWhen is it interesting to denormalize the database?
Is there any situation where having the database denormalized is more interesting than having it normalized? For example Normalized Pessoas Cidades | nome | cidade_id | | id | nome |…
-
34
votes1
answer33629
viewsWhat is the difference between ISNULL and COALESCE in a survey?
I’m having second thoughts about using the ISNULL and COALESCE. Currently creating a query in SQL Server, I was left with doubt about ISNULL and COALESCE, I did some research and I was able to find…
-
33
votes5
answers74978
viewsHow to get the table name and attributes of a Mysql database?
How to get the names of all tables in the database Mysql? How to get the attributes (name, type, etc...) of a given table in the Mysql database?
-
33
votes8
answers48170
viewsHow to calculate a person’s age in SQL Server?
Suppose the table Pessoa and the countryside DataNascimento. In an SQL query, what is the best way to calculate a person’s age in integer format in the T-SQL language of SQL Server?…
-
33
votes4
answers5507
viewsWhy are NULL values not selected?
When making a select, I noticed that the data with field NULL are not recovered using the operator<>. Why does this happen? NULL is equal to a char N? See that in the query below only the…
-
32
votes5
answers41630
viewsMysql Limit Equivalent in SQL Server
Someone knows how to relate the differences between Mysql and SQL Server, including using LIMIT n,n? Or if you have a link that lists the differences would be nice. Example SELECT * FROM tabela…
-
32
votes2
answers6682
viewsWhat would be the way to validate the CPF checker digits in a DB using only a SELECT?
Finding information on how to calculate the CPF check digits is not difficult. Any Google search gives numerous results. Most are reliable and even have a well respected source which is the entry on…
-
31
votes3
answers1472
viewsHow does an SQL Injection happen?
Why creating parameters for each field that will be updated helps prevent SQL Injection? Example: SqlCommand comm = new SqlCommand("UPDATE Contatos Set Telefone = @Telefone, " + "Cidade = @Cidade, "…
-
31
votes1
answer5487
viewsWhat is the difference between single quote ' and double quote " in SQL?
Context When I do an SQL and wish for a specific alias I should quote between double quotes ". SELECT A.ds_nome_pessoa AS "Pessoa.nome" ... When I want to make a where in field varchar the values…
-
31
votes4
answers6983
views -
31
votes5
answers12535
viewsWhy is Varchar(255) widely used?
Why in various databases, say to set the size as varchar(255)? For example, in a field email why put varchar(255)? It would not be better (in question of storage and processing speed) to put…
-
30
votes5
answers7016
viewsHow to delete all duplicates except one?
I have a table that has duplicate records. They shouldn’t be duplicated. Some records duplicated two, five and even thirty times by accident. I need to remove these duplicate records, but I need to…
-
29
votes2
answers48751
viewsWhat are Begin, Commit and Rollback transactions?
What are Begin, Commit, and Rollback Transactions? And how to use them? Example in practice will help a lot in understanding.
-
28
votes7
answers33990
viewsHow do SELECT all fields except a few?
It is known (and has been asked) which should be avoided SELECT * in some cases in bank queries. But let’s imagine that I have a table with 50 columns, and I want to select 40 of them. Just the fact…
-
24
votes1
answer2772
viewsAre unnecessary bank ratings a problem?
Indexes are usually added to improve the performance of queries. However, it is not usually recommended to add indexes in all columns, only where the need is identified. Why this recommendation?…
-
23
votes2
answers5722
viewsWhat’s wrong with the N+1?
Whenever we work with some ORM, it is common to fall into the problem of darlings N+1. It’s something about performance, called even antipattern. But what is really this problem, why it happens,…
-
22
votes3
answers1780
viewsIs the SQL language object oriented?
Is the SQL language object oriented? If so, could you show an example?
-
22
votes3
answers29017
viewsWhat is ROW_NUMBER?
What is ROW_NUMBER used in the sql server? How and where we should use it? Has a simple example of use ?
-
21
votes3
answers17170
viewsHow to generate numerical sequences in SQL without creating tables?
Reading the question How to set to zero or null in an SQL query of a date range, for those whose value does not exist? I remembered a similar problem I have. Usually when I need those sequences that…
-
21
votes3
answers1556
viewsSQL in the code or in the database?
I’m having a big doubt about where I should put mine query SQL, whether in code or bank. Currently I have a Procedure which is mounted according to the parameters I have, ie, filtros. Procedure…
-
21
votes1
answer1658
viewsHow to document SQL code?
When I write R codes, the correct way to document is in the code itself, in the form of comments initiated with a special marking #'. #' Add together two numbers. #' #' @param x A number. #' @param…
-
21
votes3
answers2676
viewsWhen to use SET and SELECT?
I know that as in virtually all issues related to "either" there must be situations where it is best to use SET or SELECT, in a Procedure (for example). At work we always wear the same SELECT, even…
-
21
votes1
answer21108
viewsWhat is the purpose of the RESTRICT, CASCADE, SET NULL and NO ACTION options?
When creating a foreign key type relationship between two tables in Mysql, I can specify some additional options in the events ON UPDATE and ON DELETE that are associated with alteration and…
-
20
votes3
answers17226
viewsDifference in Index Unique and Unique Constraint usage in Mysql?
I would like to know the behavior/differences between a Indice Unique and Unique Constraints to the Mysql ? Many may think that it makes no difference in the database, but it does! And not only for…
-
20
votes4
answers850
viewsReturn all equal items from different groups
------------------- - TABLE - ------------------- ID | GRUPO | OBJETO ---|-------|------- 1 | 1 | 1 2 | 1 | 2 | | 3 | 2 | 1 4 | 2 | 2 | | 5 | 3 | 1 6 | 3 | 2 7 | 3 | 3 | | 5 | 4 | 1 6 | 4 | 3 | | 7…
-
20
votes1
answer14650
viewsWhat are the differences between utf8 and utf8mb4?
When importing my mysql database to a windows server after having created it on a local server (xampp), I could not import into the server the script I exported from the database. So I decided to go…
-
20
votes2
answers9904
viewsWhat is the difference between clustered index and nonclustered index?
Studying about found a example, and about clustered says: Primary Keys by default use a grouped Dice (clustered), that is, when conducting a consultation select * from myTable where IDColumn = 1 ,…
-
19
votes1
answer11373
viewsHow to use a specific index in a SQL Server query?
I have a non-standard table in SQL Server installed on my PC which is fed monthly by Integration Services (SSIS) from a report in an Excel spreadsheet. I mean, I’m keeping track of a certain report.…
-
19
votes4
answers35413
viewsHow to check events between 2 dates in Mysql?
Knowing I have a table called eventos, and this table has the columns id, titulo, inicio and fim. The columns inicio and fim are of the type timestamp, where the date and time of the start and end…
-
19
votes3
answers32219
viewsFetch last 7 days data from current date
I’m making a BD query, and I want to receive results from the last 7 days from the current date. For that, I have a field data of type DATA (yyyy-mm-dd) in table. I am doing the query in php, in…
-
19
votes3
answers9682
viewsWhat’s the difference between Where and having?
Doing some tests on a database that in Mysql, I realized that: SELECT * From client WHERE uuid = '1kvsg4oracxq' returns the same result as: SELECT * From client HAVING uuid = '1kvsg4oracxq' What…
-
18
votes2
answers7922
viewsWhat is and what is the utility of the DUAL table for Oracle?
I came across this scenario during the modification of a report in the system: One of querys is returning to this such table DUAL: select * from dual But in the system there is no DUAL table, and…
-
18
votes2
answers4543
viewsWhat is an upsert?
I saw the term UPSERT in a blog and would like to better understand the functioning. It designates what type of operation? In which situations can be used? Has to do with idempotency?…
-
17
votes5
answers2401
viewsHow do I merge two results of a query?
I am developing an application in C# and I would like to know how to put together two results of two darlings SQL in one. I have the following code: public List<MalaDireta> ObterMalaDireta() {…
-
17
votes2
answers40729
viewsDifference between CROSS APPLY and OUTER APPLY?
What’s the difference between CROSS APPLY and OUTER APPLY? How do they work? In what situation can they be used? It would be possible to show some examples?
-
17
votes2
answers28085
viewsWhat are schemas? What are the advantages of using it?
In which situations your use is recommended?